无法从文件加载图像://毕加索的路径
我正尝试使用Picasso
从图库中加载图像到ImageView
。无法从文件加载图像://毕加索的路径
下面的代码
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode!= Activity.RESULT_OK) return;
switch (requestCode) {
case PICK_GALLERY_IMAGE_REQUEST: {
Uri imageUri = data.getData();
Log.d(DEBGUG_TAG,"Image URI: "+imageUri.toString());
Picasso picasso = Picasso.with(getApplicationContext());
picasso.setLoggingEnabled(true);
picasso.load(imageUri).error(R.drawable.c).into(testImgView, new com.squareup.picasso.Callback(){
@Override
public void onSuccess() {
Log.d(DEBGUG_TAG,"Success loading image from uri:PICASSO");
}
@Override
public void onError() {
Log.d(DEBGUG_TAG,"Cannot load image");
}
});
的问题是,而选择从库返回文件路径
D/debug: Image URI: file:///storage/emulated/0/DCIM/Camera/IMG_20170126_211524.jpg
这似乎并不与毕加索工作,因为返回图像错误和日志D/debug: Cannot load image
错误的方法。
但是从其他应用程序中选择相同的图像,返回Uri
如: D/debug: Image URI: content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F90455/ORIGINAL/NONE/1757236944
已成功。
任何方式从文件路径加载图像?
不要使用file://只是用它这样的文件:
Uri targetUri = data.getData();
if (data.toString().contains("content:")) {
imagePath = getRealPathFromURI(targetUri);
} else if (data.toString().contains("file:")) {
imagePath = targetUri.getPath();
} else {
imagePath = null;
}
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();
}
}
}
我正在使用File file = new File(String.value(uri));我将它改为'File file = new File(uri.getPath());'。谢谢。 –
欢迎您 –
试试下面的代码:
File imgFile = new File("/sdcard/Images/test_image.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
尝试ImageView的的内置方法setImageURI本地文件
testImgView.setImageURI(Uri.parse(imageUri));
不,相同的问题,猜测它不是关于毕加索:P –
在哪个sdk版本你的应用正在运行?请检查logcat,如果你正在越来越'FileUriExposedException' – tahsinRupam