How to open a JPG file as a BITMAP with the JPG stored on the SDCARD?

NullPointerException picture NullPointerException · Mar 2, 2012 · Viewed 45.7k times · Source

Actually i know how to open PNG files as bitmaps. But my code doens't works for open JPG files, i dont know why.

I can't find correct examples on SO or google about how to do this.

I need to have a bitmap with the JPG file opened from a dir of the sdcard. For example "sdcard/images/01.jpg"

Thanks

Answer

Bigflow picture Bigflow · Mar 2, 2012
File root = Environment.getExternalStorageDirectory();
ImageView IV = (ImageView) findViewById(R.id."image view");
Bitmap bMap = BitmapFactory.decodeFile(root+"/images/01.jpg");
IV.setImageBitmap(bMap);

Always try to use Environment.getExternalStorageDirectory(); instead of sdcard. You need an ImageView somewhere in your layout, however that's how I do this kind of things.

I use this code personally too, and it works here.