位图空指针异常
Bitmap null pointer Exception
我有从相机或图库中挑选图片的功能,但是当我尝试在 ImageView 上设置它时 returns bitmap.getWidth() 上的空指针,我也尝试使用 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
您的图片似乎存在权限问题。
open failed: EACCES (Permission denied)
如果图像是动态生成的,然后保存到临时存储中,请确保您有权从该存储中读取。另外,确保图像在保存/生成时是可读的。
还要检查以确保您的 Activity 有权访问相机中的数据
<uses-permission android:name="android.permission.CAMERA" />
最后要考虑的一件事是使用 API Level1 中的 'takePicture' 方法。
Here is the takePicture documentation
此 returns 回调的 JPEG 数据,由于从文件 URI 加载原始字节,这可能会绕过您正在执行的编码步骤。
我有从相机或图库中挑选图片的功能,但是当我尝试在 ImageView 上设置它时 returns bitmap.getWidth() 上的空指针,我也尝试使用 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
您的图片似乎存在权限问题。
open failed: EACCES (Permission denied)
如果图像是动态生成的,然后保存到临时存储中,请确保您有权从该存储中读取。另外,确保图像在保存/生成时是可读的。
还要检查以确保您的 Activity 有权访问相机中的数据
<uses-permission android:name="android.permission.CAMERA" />
最后要考虑的一件事是使用 API Level1 中的 'takePicture' 方法。 Here is the takePicture documentation
此 returns 回调的 JPEG 数据,由于从文件 URI 加载原始字节,这可能会绕过您正在执行的编码步骤。