ImageView not changing image when source is changed (already using invalidate)

南笙酒味 提交于 2019-12-25 07:10:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!