问题
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.
raw folder:
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