2

我正在开发一个应用程序,它将首先从相机拍摄的图像 uri 保存在 sqlite3 db 中,然后将其取回以将其设置为 imageview。我在 vm 中遇到了众所周知的内存不足错误。我尝试了在其他各种问题中找到的各种方法,例如:1)

那么你

either need to encourage the imageView to recycle the previous bitmap - possibly with setImageView("")
or get the bitmap from the URI and use setImageBitmap(android.graphics.Bitmap) instead; then you can do

setImageBitmap(null) 和 bitmap.recycle()。

2)

((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();

3)

protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unbindDrawables(findViewById(R.id.showDataViewRoot));
        System.gc();

    }

奇怪的是,它们都不起作用,并且错误不断出现。主要是因为错误出现在第一次尝试显示图像本身时(所以我猜没有机会回收)。搜索时,我发现以下链接: http : //www.vogella.com/articles/AndroidCamera/article.html#example 从那里得到提示,我做了以下更改:

imageView.setImageBitmap(BitmapFactory.decodeStream(new FileInputStream(path)));

为我的 ShowData.java 完成修改后的代码(带有注释的先前尝试)。在 fillImage() 中设置图像的代码:

package org.dheeraj.imnci;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ShowData extends Activity {
    private Spinner spinnerId;
    private DbHelper dbHelper;
    private SQLiteDatabase dbReader;
    private Cursor cursor;
    private ArrayList<String> idList;
    private ArrayAdapter<String> adapter;
    private String id;
    private TableLayout dataTable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_data);
        dbHelper = new DbHelper(this);
        spinnerId = (Spinner) findViewById(R.id.spinnerData);
        dataTable = (TableLayout) findViewById(R.id.tableShowData);
        fillSpinner();

    }

    private void fillData(String id) {
        String tableNames[] = { "mother_reg", "anc_02", "anc_03", "anc_04",
                "tt1", "tt2", "ttb", "abortions", "po", "pnc", "ifa" };
        for (String table : tableNames) {
            getDataFromTable(table, id);
        }
    }

    private void getDataFromTable(String table, String id) {
        // TODO Auto-generated method stub

        getTableTitle(table);
        getTableColumns(table, id);
    }

    private void getTableColumns(String table, String id) {
        // TODO Auto-generated method stub
        SQLiteDatabase dbReader;
        Cursor cursor;
        TableRow colRow;
        TextView labelView;
        TextView valueView;
        String label;
        String value;

        dbReader = dbHelper.getReadableDatabase();
        try {
            if (table.equals("mother_reg"))
                cursor = dbReader.query(table, null, "mid=" + id, null, null,
                        null, null, null);
            else
                cursor = dbReader.query(table, null, "ID=" + id, null, null,
                        null, null, null);

            Log.d("getTableColumns", table);
            if (cursor.moveToFirst()) {
                do {
                    Log.d("in cursor", "" + cursor.getColumnCount());
                    for (int i = 0; i < cursor.getColumnCount(); i++) {
                        label = cursor.getColumnName(i);
                        value = cursor.getString(i);

                        labelView = new TextView(this);
                        valueView = new TextView(this);
                        colRow = new TableRow(this);

                        labelView.setText(label);
                        valueView.setText(value);
                        colRow.addView(labelView);
                        colRow.addView(valueView);
                        dataTable.addView(colRow);
                    }

                } while (cursor.moveToNext());

            }
            cursor.close();
        } finally {
            if (dbReader != null)
                dbReader.close();
        }
    }

    private void getTableTitle(String table) {
        // TODO Auto-generated method stub
        TableRow tabRow;
        TextView tv;
        String title = table.replace('_', ' ').toUpperCase();
        tabRow = new TableRow(this);
        tv = new TextView(this);
        tv.setText(title);
        tv.setTextSize(20);
        tabRow.addView(tv);
        dataTable.addView(tabRow);
    }

    private void fillImage(String id) {
        // TODO Auto-generated method stub

        ImageView imageView;
        boolean picFound;

        SQLiteDatabase dbReader;
        Cursor cursor;

        String path = null;

        imageView = (ImageView) findViewById(R.id.showDataImage);

        picFound = false;
        dbReader = dbHelper.getReadableDatabase();
        try {
            String tableName = "pictures";
            String[] columns = { "mid", "uri" };
            Log.d("id value", "" + id);
            cursor = dbReader.query(tableName, columns, "mid=" + id, null,
                    null, null, null, null);
            Log.d("gotCursor", "foundcursor");
            if (cursor.moveToFirst()) {

                Log.d("in cursor", "" + cursor.getColumnCount());
                path = cursor.getString(cursor.getColumnIndex("uri"));
                Log.d("show_data:imagepath", path);
                if (path != null)
                    picFound = true;
                // imgUri = Uri.parse(new File(path).toString());
                /*
                 * bmpImage = BitmapFactory.decodeFile(path);
                 * 
                 * 
                 * 
                 * scaledBmp = Bitmap.createScaledBitmap(bmpImage, 100, 150,
                 * true); bmpImage.recycle(); bmpImage = null;
                 */

            }
            cursor.close();
        } finally {
            if (dbReader != null)
                dbReader.close();
        }

        if (!picFound)
            imageView.setImageResource(R.drawable.default_user);
        else {
            // Log.d("inimageview", imgUri.toString());

            try {
                imageView.setImageBitmap(BitmapFactory.decodeStream(new FileInputStream(path)));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        // LayoutParams lv = new LayoutParams(200, 300);
        // imgRow.setLayoutParams(lv);

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unbindDrawables(findViewById(R.id.showDataViewRoot));
        System.gc();

    }

    private void unbindDrawables(View view) {
        if (view.getBackground() != null) {
            view.getBackground().setCallback(null);
        }
        if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
            ((ViewGroup) view).removeAllViews();
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unbindDrawables(findViewById(R.id.showDataViewRoot));
        System.gc();
    }

    void fillSpinner() {
        dbReader = dbHelper.getReadableDatabase();
        String columns[] = { "mid" };

        try {
            cursor = dbReader.query("mother_reg", columns, null, null, null,
                    null, "mid DESC");
            if (cursor.moveToFirst()) {
                idList = new ArrayList<String>();
                do {
                    idList.add(cursor.getString(cursor.getColumnIndex("mid")));
                } while (cursor.moveToNext());
                cursor.close();

                adapter = new ArrayAdapter<String>(this,
                        android.R.layout.simple_spinner_item, idList);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnerId.setAdapter(adapter);
                spinnerId
                        .setOnItemSelectedListener(new OnItemSelectedListener() {

                            @Override
                            public void onItemSelected(AdapterView<?> arg0,
                                    View arg1, int arg2, long arg3) {
                                // TODO Auto-generated method stub
                                id = arg0.getItemAtPosition(arg2).toString();
                                // dataTable.removeAllViews();
                                unbindDrawables(dataTable);
                                fillImage(id);
                                fillData(id);
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> arg0) {
                                // TODO Auto-generated method stub

                            }
                        });
            }
        } finally {
            if (dbReader != null)
                dbReader.close();
        }
    }
}

我无法理解的是它是如何工作的。与在 imageview.setimageuri(Uri.parse(path)) 中直接使用文件相比,采用输入流是否会以某种方式减小大小,或者它使用某种缓冲区?只是想知道出了什么问题。请告知是否需要有关我的应用程序代码的更多信息。但是这个奇怪的内存问题真的让我感到困惑,我真的对不同线程中提到的这么多方法感到困惑。关于我的应用程序的更多信息:目标 api:1.6 中间 SDK 版本:4

编辑 1:我很抱歉,但在使用 ShowData 进行一些试验后,这种方法也失败了。以下是 Logcat 的屏幕截图: 在此处输入图像描述 现在我真的很困惑。只是如何在我的图像视图中显示相机保存在 sdcard 中的简单图像?我真诚地感谢您的任何帮助。

编辑 2:尝试在 fillimage 视图方法中进行更多更改:

        try {
            if(((BitmapDrawable)imageView.getDrawable())!=null)
            ((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();



            imageView.setImageBitmap(BitmapFactory.decodeStream(new FileInputStream(path)));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

它现在工作顺利,除了它给出了以下错误: 在此处输入图像描述 我现在真的很想找到这件事的根源。请提供任何指导。对于这么多的编辑,我感到非常抱歉,但我想继续告知任何进一步的进展。

4

1 回答 1

1

如果您想全屏显示单个图像,请使用 Intent.ACTION_VIEW 启动活动:

Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(uri, "image/jpeg");
startActivity(i);

如果要显示缩略图,然后通过二次采样减小这些图像的大小,只需将此选项添加到 BitmapFactory:http: //developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize

编辑1:

有很好的文档:http:
//developer.android.com/training/displaying-bitmaps/load-bitmap.html

编辑2:根据你的第二个问题:

Bitmap oldBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
imageView.setImageDrawable(null); //this should help
oldBitmap.recycle();
imageView.setImageBitmap(newBitmap);
于 2012-06-11T08:41:50.350 回答