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);
}
Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • You don't need `invalidate()` there because `setImageBitmap()` will redraw anyway. Are you doing anything with it in `onResume()`? – codeMagic Jul 31 '13 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. – Geobits Jul 31 '13 at 18:22
  • Thanks, both of you, I have removed the redundant code, and will repost updated code. – Rilcon42 Jul 31 '13 at 20:08

2 Answers2

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.

Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
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)

Benito Bertoli
  • 25,285
  • 12
  • 54
  • 61
  • That fixed it. I didn't realize that it would return a null image instead of null. Thanks very much! – Rilcon42 Jul 31 '13 at 20:13