库图像文件存在,但位图解码文件为空
我正在尝试显示使用OnActivityResult
返回的uri路径从图库中拾取的图像.该文件存在并且为.png
或.jpg
格式,但Bitmap.decodeFile()
始终为null或在Access denied error
中显示了一些图片(已保存facebook Messenger).我究竟做错了什么?
I'm trying to show an image picked from the gallery using the uri path returned by OnActivityResult
.The file exists and it is either a .png
or .jpg
format but Bitmap.decodeFile()
is always null or for some pictures(facebook messenger saved) it's showin an Access denied error
. What am I doing wrong?
我确实阅读了所有相关主题.
I did read all of the related topics.
我不想使用正在工作的openStream
方法或也正在工作的imageView.setImageURI
选项.
I don't want to use the openStream
method which was working, or the imageView.setImageURI
option which was also working.
清单:
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="ANDROID.PERMISSION.READ_EXTERNAL_STORAGE"/>
MainActivity onCreate:
MainActivity onCreate:
// required by Android 6.0 +
checkPermissions(getApplicationContext());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent gallery =
new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGES);
}
});
MainActivity onActivitiyResult:
MainActivity onActivitiyResult:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == PICK_IMAGES && resultCode == RESULT_OK && data != null) {
String imagePath = "";
Uri targetUri = data.getData();
if (data.toString().contains("content:")) {
imagePath = getRealPathFromURI(targetUri);
} else if (data.toString().contains("file:")) {
imagePath = targetUri.getPath();
} else {
imagePath = null;
}
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Log.d("Image",imagePath);
File thePath = new File(imagePath);
if(thePath.exists()){
Log.d("Image", "exists");
}
Bitmap myBitmap = BitmapFactory.decodeFile(thePath.getAbsolutePath());
if(myBitmap == null)
Log.d("Image","bummer");
imageView.setImageBitmap(myBitmap);
} else {
Log.d(TAG, "Something went wrong");
}
}
public String getRealPathFromURI(Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = getContentResolver().query(contentUri, proj, null, null,
null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
我做错了什么?
What am I doing wrong?
首先,Android区分大小写,因此您的<uses-permission>
元素错误.将ANDROID.PERMISSION
替换为android.permission
.
First, Android is case-sensitive, so your <uses-permission>
elements are wrong. Replace ANDROID.PERMISSION
with android.permission
.
第二,不要求您的getRealPathFromURI()
方法在所有设备上的所有图像均适用.替换:
Second, there is no requirement for your getRealPathFromURI()
method to work on all devices for all images. Replace:
String imagePath = "";
Uri targetUri = data.getData();
if (data.toString().contains("content:")) {
imagePath = getRealPathFromURI(targetUri);
} else if (data.toString().contains("file:")) {
imagePath = targetUri.getPath();
} else {
imagePath = null;
}
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Log.d("Image",imagePath);
File thePath = new File(imagePath);
if(thePath.exists()){
Log.d("Image", "exists");
}
Bitmap myBitmap = BitmapFactory.decodeFile(thePath.getAbsolutePath());
if(myBitmap == null)
Log.d("Image","bummer");
imageView.setImageBitmap(myBitmap);
具有:
Uri targetUri = data.getData();
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap myBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
if(myBitmap == null)
Log.d("Image","bummer");
imageView.setImageBitmap(myBitmap);
最终,使用图像加载库(例如Picasso,Glide)或将其移至后台线程.
Eventually, use an image-loading library (e.g., Picasso, Glide) or otherwise move that work to a background thread.