TJ1
TJ1

Reputation: 8468

Android: setImageBitmap for ImageView causes crash

I am trying to set Image Bitmap of an ImageView to a bitmap. Here is the code:

my_image_view = (ImageView) findViewById(R.id.imageView);
Bitmap bmp_image = getOutputBitmap();
my_image_view.setImageBitmap(bmp_image);

The getOutputBitmap reads an image and returns a bitmap. However, the 3rd line of the code, i.e my_image_view.setImageBitmap(bmp_image); causes the app to crash.

Does the bmp_image need to have any specific format or properties? The crash does not come from a null value for bmp_image as I see it is not null in debug mode, actually, it is a real image and I can watch it in debug mode.

Thank you very much for your help in advance.

Upvotes: -1

Views: 1095

Answers (1)

Dhaval Solanki
Dhaval Solanki

Reputation: 4705

Actually problem is not in your third line for setup a Bitmap but I think your method returning null bitmap so before checking null condition it will not crash, like following

my_image_view = (ImageView) findViewById(R.id.imageView);
Bitmap bmp_image = getOutputBitmap();
if(bmp_image!=null)
  my_image_view.setImageBitmap(bmp_image);

Upvotes: 0

Related Questions