0

I have an imageView with a set image in xml. However when I try to change the image the ImageView never displays the second image. Instead it displays a blank screen. I have tried the code with and without invalidate() and there is no change.

ImageView image; String imgPath=Environment.getExternalStorageDirectory()+"/Music/Evanescence - Discography - 1998-2011 (320 kbps)/Album's/2000 Origin (Limited Edition) (320 kbps)/Scans covers/06.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image=(ImageView)findViewById(R.id.imageView1); image.setImageBitmap(BitmapFactory.decodeFile(imgPath)); if(!new File(imgPath).exists()) Log.i("aaa","File doesnt exist"); //select image from gallery //Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); //photoPickerIntent.setType("image/*"); //startActivityForResult(photoPickerIntent, SELECT_PHOTO); Log.i("aaa","DONE. img path: "+imgPath); } 
3
  • You don't need invalidate() there because setImageBitmap() will redraw anyway. Are you doing anything with it in onResume()? Commented Jul 31, 2013 at 18:17
  • Side note: You don't need to call BitmapFactory.decodeFile(imgPath) twice to null-check it. If it returns null on the second call for some reason, you won't get that "bitmap was null" message anyway. Commented Jul 31, 2013 at 18:22
  • Thanks, both of you, I have removed the redundant code, and will repost updated code. Commented Jul 31, 2013 at 20:08

2 Answers 2

0

I think the image is too large.

On some devices, instead of java.lang.OutofMemoryError being thrown, a null bitmap is returned.

Try running your code with a smaller image. If that works, you have to learn how to display bitmaps properly in Android.

(Well either way you have to)

Sign up to request clarification or add additional context in comments.

1 Comment

That fixed it. I didn't realize that it would return a null image instead of null. Thanks very much!
0

Are you setting android:background="someDrawable" in xml? Check that and remove or set it as android:src="drawable"

If setImageBitmap is not working try with setBackground(drawable)

I know this is not the perfect answer but just a workr-around.. We will find find a definite answer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.