2

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

Jovan
  • 4,684
  • 13
  • 64
  • 98
  • 2
    Why don't you want to use drawable folder ? – romtsn Feb 16 '15 at 19:05
  • because drawable scaled down images depend of device – Jovan Feb 16 '15 at 19:12
  • If you only add 1 image to 1 type of drawable, you won't get autoscale. But you can put an image file in raw, you're just going to have to open it using BitmapFactory and then pass the bitmap to the imageview. – Gabe Sechan Feb 16 '15 at 19:13
  • 2
    In that case you can create a `drawable-nodpi` folder and put your image there and it won't be scaled – romtsn Feb 16 '15 at 19:14

2 Answers2

5

You can try it

   InputStream imageStream = this.getResources().openRawResource(R.raw.name);
   Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
   imageview.setImageBitmap(bitmap);
Eugene Lezov
  • 722
  • 7
  • 12
4

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491