Jovan
Jovan

Reputation: 4794

Can't load image from raw to imageview

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

Upvotes: 2

Views: 7418

Answers (2)

Eugene Lezov
Eugene Lezov

Reputation: 770

You can try it

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

Upvotes: 6

CommonsWare
CommonsWare

Reputation: 1007534

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.

Upvotes: 5

Related Questions