Android 拾取图片时BitmapFactory.decode文件出现问题
拾取图片时,BitmapFactory.decode文件返回空值。 以下是我的功能:Android 拾取图片时BitmapFactory.decode文件出现问题,android,android-studio,Android,Android Studio,拾取图片时,BitmapFactory.decode文件返回空值。 以下是我的功能: protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Here we need to check if the activity that was triggers was
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Here we need to check if the activity that was triggers was the Image Gallery.
// If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
// If the resultCode is RESULT_OK and there is some data we know that an image was picked.
if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && data != null) {
// Let's read picked image data - its URI
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath,options);
//imageView.setImageBitmap(bitmap);
// Do something with the bitmap
// At the end remember to close the cursor or you will end with the RuntimeException!
cursor.close();
}
}
有人能告诉我问题出在哪里吗?
stringimagepath=cursor.getString(cursor.getColumnIndex(filePath[0])代码>
在变量imagePath中是否有文件路径?或者imagePath值也为空?
更新
您的BitmapFactory.decodeFile()返回null,原因可能是imageSize太大,并且引发了一些异常
检查日志,查看是否存在outOfMemory位图错误。让我知道你能更具体地说明你想要实现的目标吗?我试着从图库中挑选一张图片,然后得到它的位图是的,我有一个imagePath,这是我获得的路径示例:/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20170217-WA0001.jpgI获取此错误:/BitmapFactory:无法解码流:java.io.FileNotFoundException:/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20170217-WA0001.jpg:打开失败:但我在清单:您是否正在尝试API级别>=23?是的,我使用的是API=23,您使用的设备高于棒棒糖,如果是,您是否提供了运行时权限?