I have function that pick a picture from camera or gallery but when I try to set on an ImageView it returns Null pointer on bitmap.getWidth(), I also try to use decodeStream instead decodeFile but I get errors in the path variable, how can I solve that? Im testing on a Nexus 6, BTW my english is not good.
thanks in advance!
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
if (resultCode != RESULT_OK) return;
Bitmap bitmap = null;
String path = "";
if (requestCode == PICK_FROM_FILE) {
mImageCaptureUri = data.getData();
path = getRealPathFromURI(mImageCaptureUri); //from Gallery
if (path == null)
path = mImageCaptureUri.getPath(); //from File Manager*/
if (path != null)
bitmap = BitmapFactory.decodeFile(path);
} else {
path = mImageCaptureUri.getPath();
bitmap = BitmapFactory.decodeFile(path);
}
final double viewWidthToBitmapWidthRatio = (double) mImageView.getWidth() / (double) bitmap.getWidth();
mImageView.getLayoutParams().height = (int) (bitmap.getHeight() * viewWidthToBitmapWidthRatio);
mImageView.setImageBitmap(bitmap);
}
this is the log:
FATAL EXCEPTION: main
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/tmp_avatar_1450385165853.jpg: open failed: EACCES (Permission denied)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference