问题
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);
}
回答1:
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)
回答2:
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.
来源:https://stackoverflow.com/questions/17977872/imageview-not-changing-image-when-source-is-changed-already-using-invalidate