Reputation: 12592
public static Bitmap loadBitmap(Context context, String filename){
AssetManager assets = context.getResources().getAssets();
InputStream buf = null;
try {
buf = new BufferedInputStream((assets.open("drawable/black_circle.png")));
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(buf);
return bitmap;
}
when I use the code above the system gives the following error
09-27 12:33:44.470: W/System.err(18554): java.io.FileNotFoundException: drawable/black_circle.png
09-27 12:33:44.470: W/System.err(18554): at android.content.res.AssetManager.openAsset(Native Method)
09-27 12:33:44.470: W/System.err(18554): at android.content.res.AssetManager.open(AssetManager.java:315)
09-27 12:33:44.470: W/System.err(18554): at android.content.res.AssetManager.open(AssetManager.java:289)
I'm running the code on a Samsung Galaxy, through Eclipse. How to make this work?
I need to change the file name dynamically.
Upvotes: 0
Views: 223
Reputation: 5258
Try this way :
int image_id = getResources().getIdentifier("imagename", "drawable", getPackageName());
image.setBackgroundResource(image_id);
Upvotes: 2
Reputation: 1325
You can try :
InputStream is = (InputStream) context.getResources().openRawResource(R.drawable.black_circle);
Upvotes: 0
Reputation: 759
Try this.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.addto);
if you want to create bitmap from the drawable image then you can use this.
Upvotes: 0
Reputation: 15973
to open images under drawable folder:
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.black_circle);
Upvotes: 1