Reading an image file into bitmap from sdcard, why am I getting a NullPointerException? Reading an image file into bitmap from sdcard, why am I getting a NullPointerException? android android

Reading an image file into bitmap from sdcard, why am I getting a NullPointerException?


The MediaStore API is probably throwing away the alpha channel (i.e. decoding to RGB565). If you have a file path, just use BitmapFactory directly, but tell it to use a format that preserves alpha:

BitmapFactory.Options options = new BitmapFactory.Options();options.inPreferredConfig = Bitmap.Config.ARGB_8888;Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);selected_photo.setImageBitmap(bitmap);

or

http://mihaifonoage.blogspot.com/2009/09/displaying-images-from-sd-card-in.html


It works:

Bitmap bitmap = BitmapFactory.decodeFile(filePath);


Try this code:

Bitmap bitmap = null;File f = new File(_path);BitmapFactory.Options options = new BitmapFactory.Options();options.inPreferredConfig = Bitmap.Config.ARGB_8888;try {    bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options);} catch (FileNotFoundException e) {    e.printStackTrace();}         image.setImageBitmap(bitmap);