BitmapFactory.decodeFile在Android 4.4 KitKat上返回异常
我想使用以下代码显示图像:BitmapFactory.decodeFile在Android 4.4 KitKat上返回异常,android,android-bitmap,Android,Android Bitmap,我想使用以下代码显示图像: protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case SELECT_PHOTO:
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Log.wtf("M3K", "Above decode");
Bitmap logoBMP = BitmapFactory.decodeFile(filePath);
Log.wtf("M3K", "Below decode");
//Display image on layout
Log.wtf("M3K", "Above display");
logo.setImageBitmap(logoBMP);
Log.wtf("M3K", "Below display");
}
}
}
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
问题是关于Bitmap logoBMP=BitmapFactory.decodeFile(filePath)代码>在Android 4.4(在我的Nexus 7上测试)上,它将返回一个文件未找到异常,原因是EACCES(权限被拒绝)。这在运行4.2的华硕Transformer Infinity上非常有效,在运行4.3的Nexus 7上也非常有效。有人知道KitKat兼容性需要做哪些更改吗
注意:我通过以下代码获得图像URI:
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Log.wtf("M3K", "Above decode");
Bitmap logoBMP = BitmapFactory.decodeFile(filePath);
Log.wtf("M3K", "Below decode");
//Display image on layout
Log.wtf("M3K", "Above display");
logo.setImageBitmap(logoBMP);
Log.wtf("M3K", "Below display");
}
}
}
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
您可以尝试将以下内容放入您的文件中:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
您可以尝试将以下内容放入您的文件中:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
permission denied means,尝试给出WRITE\u EXTERNAL\u storage permission denied means,尝试给出WRITE\u EXTERNAL\u storage非常感谢!我真不敢相信这个解决方案这么简单。非常感谢!我真不敢相信解决办法这么简单。