无法从Picasso中的file://路径加载图像

Sal*_*mim 1 android uri image android-contentresolver picasso

我正在尝试将图像加载到ImageView库中Picasso.

这是代码

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");
                    }
                });
Run Code Online (Sandbox Code Playgroud)

问题是从图库中选择图像时,它返回文件路径

D/debug: Image URI: file:///storage/emulated/0/DCIM/Camera/IMG_20170126_211524.jpg

这似乎不适用于Picasso作为返回错误并记录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成功.

有没有办法从文件路径加载图像?

Saf*_*ire 6

不要使用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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)