在android中设置imageview位图时的奇怪行为

时间:2012-06-11 08:06:15

标签: android memory imageview virtual-machine

我正在开发一个应用程序,它首先在sqlite3数据库中保存从摄像头获取的图像,然后将其恢复以将其设置为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))或它使用某种缓冲区?只是想知道出了什么问题。请告诉我们是否需要有关我的应用程序代码的更多信息。但是这个奇怪的记忆问题让我很困惑,而且我对在不同主题中提到的很多方法感到困惑。 有关我的应用的更多信息: target api:1.6 mid SDK版本:4

编辑1:我很抱歉,但在使用ShowData进行一些试验后,这种方法也失败了。以下是Logcat的屏幕截图: enter image description here 现在我真的很困惑。在我的imageview中如何在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();
        }

现在它的工作顺利,除非它发出以下错误: enter image description here 我现在真的想谈谈这件事的根源。请提供任何指导。我很抱歉这么多的编辑,但我想继续告知任何进一步的进展。

1 个答案:

答案 0 :(得分: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);