Android openRawResource() not working for a drawable

混江龙づ霸主 提交于 2019-12-18 20:29:28

问题


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???


回答1:


@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!




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/25572647/android-openrawresource-not-working-for-a-drawable

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