Reputation: 802
i have this snippet to retieve an image from an sd car card but all i get is java.lang.nullPointerException:
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inSampleSize = 2;
options.inTempStorage = new byte[16*1024];
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().toString() + "/monImage.png", options);
image.setImageBitmap(bitmap);
// selected_photo = (ImageView) findViewById(R.id.selected_photo);
/*String photoPath="/"+ Environment.getExternalStorageDirectory().toString() + "/monImage.png";
Uri photoUri = Uri.parse(photoPath);
image.setImageBitmap(MediaStore.Images.Media.getBitmap(getContentResolver(),photoUri));*/
}
catch (Exception e){Log.d("merde>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "grrrrr " +e);}
I have tried many oder solutions but still, i always have the same problem even when i change the image file. And when i change the file name to give a false one, it tells me that file not found :(
can anyone help?? i've seen others having the same problem but didn't see anysolution that worked for me
Upvotes: 0
Views: 122
Reputation: 3036
I tried it and this works for me:
Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/monImage.png");
If you have more problems you may want to the state of the external storage:
boolean mExternalStorageAvailable, mExternalStorageWriteable;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state))
{ mExternalStorageAvailable = true;
mExternalStorageWriteable = true;
}
else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
{ mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
}
else
{ mExternalStorageAvailable = false;
mExternalStorageWriteable = false;
}
Upvotes: 1