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
It appears that you have a Permissions problem with the image.
open failed: EACCES (Permission denied)
If the image is being generated dynamically and then saved to a temporary storage, insure you have the right to read from that storage. Also, make sure the image is readable when being saved / generated.
Also check to insure that your Activity has permission to access data from the camera
<uses-permission android:name="android.permission.CAMERA" />
One last thing to consider, is using the 'takePicture' method from API Level1. Here is the takePicture documentation
This returns the JPEG data to the callback, which may bypass the encoding step you're taking due to loading raw bytes from the file URI.