我想使用以下代码显示图像:
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");
}
}
}
问题在于Bitmap logoBMP = BitmapFactory.decodeFile(filePath);
在 Android 4.4 上(在我的 Nexus 7 上测试)它将返回一个文件未找到异常,原因是EACCES (Permission denied)。这在运行 4.2 的 ASUS Transformer Infinity 上完美运行,并在运行 4.3 的 Nexus 7 上完美运行。有谁知道 KitKat 兼容性需要改变什么?
注意:我通过以下代码获取图像 URI:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);