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