12

I'm trying to create an input stream by doing this

InputStream is = (InputStream) getResources().openRawResource(R.drawable.image1);

but I'm met with the error "Expected resource of type raw" with respect to my drawable file (R.drawable.image1). image1 is a png and in my res/drawable folder.

Any ideas???

zmarties
  • 4,809
  • 22
  • 39
BoltzmannBrain
  • 5,082
  • 11
  • 46
  • 79

3 Answers3

38

@Broatian I don't currently have a res/raw folder. I found an alternative solution: is = context.getResources().openRawResource(+ R.drawable.image1); The + shows additional folders. Thanks for the help!

BoltzmannBrain
  • 5,082
  • 11
  • 46
  • 79
  • 5
    What is the significance of this plus? This does not seem like standard Java syntax to me - is it? How does it affect the Android compiler? – Atorian Nov 11 '14 at 11:20
  • 4
    What's going on here? Why does this work? This clearly works. – maligree Mar 27 '15 at 09:47
  • @alavin The plus sign works. Does anybody have a link to some documentation of what this plus sign does? – tomi Apr 02 '16 at 07:54
  • 2
    @tomi @maligree That's the unary plus operator: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html In the latest Android Studio, I got it to work with a `0 + R.drawable.blah`. I think what's going on is it's tricking the IDE into no longer knowing precisely what "type" of int is being passed into the function, so it can't complain about not receiving an id referencing a "raw" resource. Kind of annoying that the IDE is being so overly aggressive, since the docs for `openRawResource` say that `it can be used to open drawable, sound, and raw resources`. – qix May 12 '16 at 06:26
5

If you want to open your png with this method then you have to put it in the res/raw folder. Then you'll be able to access the png using R.raw.image1.

Another alternative is to keep it in the drawable folder and then convert the drawable to a InputStream. See How to read drawable bits as InputStream

I assume you know how to get the drawable otherwise look at the Resources documentation.

Community
  • 1
  • 1
Broatian
  • 860
  • 9
  • 10
0

It is because They are expecting a raw type resource and you are passing a drawable type one. make a raw folder in the res folder and then put your image in the raw folder.

arnav bhartiya
  • 303
  • 1
  • 3
  • 9