我有从相机或图库中选择图片的功能,但是当我尝试在 ImageView 上设置时,它会在 bitmap.getWidth() 上返回 Null 指针,我也尝试使用 decodeStream 而不是 decodeFile 但我在路径变量中遇到错误,怎么能我解决?我在 Nexus 6 上进行测试,顺便说一句,我的英语不好。
提前致谢!
@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);
}
这是日志:
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