Issue
I am having a weird problem. I am trying to use a PNG file from my res/drawable folder within my Android project, and to use such a PNG file in a ImageButton (which has the method "setImageBitmap(Bitmap bm)") I need to decode the PNG image which I have in my res/drawable file. However, when I copy the path and try to decode it like such:
public Bitmap getBitMap() {
return BitmapFactory.decodeFile("drawable/bishop00.png");
}
I saw some threads that said that I could also use the "decodeResources" method of BitmapFactory, but this code is not in an activity, and I do not wish to use an activity as a parameter to just use that method, seeing as this should work. I have also tried using the absolute path to this (copying the path which shows the entire hierarchy of my system) but I still get the following exception:
Unable to decode stream: java.io.FileNotFoundException
Really hope to get some help, seeing as I am completely sure the file exists.
Solution
I am trying to use a PNG file from my res/drawable folder within my Android project
That is a file on your development machine. It is not a file on the Android device.
seeing as this should work
No, it should not.
I saw some threads that said that I could also use the "decodeResources" method of BitmapFactory
That is the correct answer, assuming that this content should be a drawable resource in the first place. While there are some valid reasons for using decodeResource()
, it is not that commonly used overall. There may be other ways of using the drawable resource that are more appropriate, or perhaps this should not be a drawable resource in the first place.
but this code is not in an activity
decodeResource()
takes a Context
. It does not have to be an Activity
, though that usually is a likely candidate. All of your code is executed along some code path that started with a Context
; you should have no problems having one for use here.
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.