Android: FileNotFoundException On An Image In Drawable
I'm trying to use this code that I've seen on multiple posts throughout stackoverflow public static Bitmap loadBitmap(Context context, String filename) throws IOException {
Solution 1:
The AssetManager
is intended to access the data in the assets
folder. So in your example, it looks for assets/drawable/filename
and does not find anything.
To get a resource from drawable, one should use
Drawable d = getResources().getDrawable(R.drawable.filename);
If you're sure it's a bitmap, you can do it like so:
Bitmap bm = ((BitmapDrawable)getResources()
.getDrawable(R.drawable.filename)).getBitmap();
Solution 2:
You can do it like this...
// load image from Assets folder
// get input stream
InputStream mIms = getAssets().open("ic_launcher.png");
// load image as Drawable
Drawable mDraw = Drawable.createFromStream(mIms, null);
// set image to ImageView
mIms.setImageDrawable(mDraw);
Post a Comment for "Android: FileNotFoundException On An Image In Drawable"