Can't load image from raw to imageview

风格不统一 提交于 2021-01-28 05:53:49

问题


I want to load image using setImageResource, from raw folder to imageview, But I get Expected resource of type drawable error.

I'm using android studio, Before that when I was using Eclipse everything was fine. I don't want to use drawable folder.

Error

raw folder:

raw folder in my project

Any suggestion? Thanks


回答1:


I want to load image using setImageResource, from raw folder to imageview, But I get Expected resource of type drawable error.

Using a raw resource for setImageResource() is not documented behavior.

Before that when I was using Eclipse everything was fine

Not really. While your code runs, it might not on all past, present, and future versions of Android. This is why there is a compiler warning.

because drawable scaled down images depend of device

Use drawable-nodpi for drawables that should not be scaled based upon screen density. That is usually not a good idea, but perhaps you have a reason for it.

A safer approach for using a raw resource would be to use openRawResource() on Resources to get an InputStream, then use BitmapFactory to read in the bitmap.

You are welcome to suppress the warning with @SuppressWarnings("ResourceType"), but do not complain when your app stops working.




回答2:


You can try it

   InputStream imageStream = this.getResources().openRawResource(R.raw.name);
   Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
   imageview.setImageBitmap(bitmap);


来源:https://stackoverflow.com/questions/28548475/cant-load-image-from-raw-to-imageview

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