0

I am trying to set an image to an imageview in a custom dialog with a drawable. I have the following method

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
        this.setCancelable(false);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        ViewGroup vg = (ViewGroup)inflater.inflate(R.layout.popup, null);
        image= (ImageView) vg.findViewById(R.id.image);
        Uri uri = Uri.parse("android.resource://"+this.getActivity().getPackageName()+"/drawable/p1");
        image.setImageURI(uri);
.
.
return builder.create();
}

It runs fine most of time but causes a Out of memory on a xxxx-byte allocation.

I know it is because of this

image.setImageURI(uri);

What is the best way to get rid of this problem??

UPDATE::

I tried to recycle the bitmap by using this 

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
        if(!bitmap.isRecycled()){
        bitmap.recycle();
        bitmap =null;
        }

Now If i get a dialong with same image consecutively I have this error:

Canvas trying to use a recycled bitmap Runtime Exception. 

Any help is appreciated

4

2 に答える 2

0

最善の方法は、アプリが使用しているメモリの量と使用している場所を調べることです。おそらくどこかで漏れています。そうでない場合は、全体的なメモリ使用量を減らす方法を見つけてください。Eclipse は、ヒープ使用量のダンプを取得できます。

于 2013-02-21T21:17:23.550 に答える
0

Eclipse メモリ アナライザー ( http://www.eclipse.org/mat/ ) は、DDMS およびヒープ アナライザーと組み合わせて、リークを見つけるのに役立ちます。

ヒープの更新を開始するには、Eclipse 内から DDMS ビューに切り替えることができます。そこでアプリに対応するプロセスを選択し、[ヒープの更新を表示] ボタンを選択します。その後、「Cause GC」ボタンをクリックするたびに、ヒープ上のオブジェクトの更新が表示されます。

これを Eclipse で分析するには、[Dump HPROF file] をクリックして Eclipse メモリ アナライザーにロードします。これにより、何がリークしているのかについてより多くのヒントが得られます。

Android 開発者のブログのこのブログ投稿では、さらに詳しく説明しています: http://android-developers.blogspot.ca/2011/03/memory-analysis-for-android.html

于 2013-02-21T23:38:50.073 に答える