Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How can I read an image file into bitmap from sdcard?

 _path = Environment.getExternalStorageDirectory().getAbsolutePath();    System.out.println("pathhhhhhhhhhhhhhhhhhhh1111111112222222 " + _path);   _path= _path + "/" + "flower2.jpg";   System.out.println("pathhhhhhhhhhhhhhhhhhhh111111111 " + _path);   Bitmap bitmap = BitmapFactory.decodeFile(_path, options );   

I am getting a NullPointerException for bitmap. It means that the bitmap is null. But I have an image ".jpg" file stored in sdcard as "flower2.jpg". What's the problem?

like image 753
Smitha Avatar asked Jan 03 '12 10:01

Smitha


People also ask

How do I convert a bitmap to a string?

If you have a string base64 and need to convert it to a bitmap, this is the simplest and natural method. Code: String base64String = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAA..."; String base64Image = base64String.


2 Answers

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

like image 143
NikhilReddy Avatar answered Oct 13 '22 10:10

NikhilReddy


It works:

Bitmap bitmap = BitmapFactory.decodeFile(filePath); 
like image 34
Ahmad Arslan Avatar answered Oct 13 '22 10:10

Ahmad Arslan