【问题标题】:Cannot load Image from file:// path in Picasso无法从毕加索的 file:// 路径加载图像
【发布时间】:2017-04-10 07:35:53
【问题描述】:

我正在尝试使用 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

这似乎不适用于 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 成功。

有什么方法可以从文件路径加载图片?

【问题讨论】:

  • 您的应用在哪个 sdk 版本中运行?如果您收到 FileUriExposedException,请检查 logcat

标签: android image uri picasso android-contentresolver


【解决方案1】:

不要使用 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()); 时它起作用了。谢谢。
【解决方案2】:

试试下面的代码:

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);

}

【讨论】:

    【解决方案3】:

    尝试ImageView的内置方法setImageURI获取本地文件

    testImgView.setImageURI(Uri.parse(imageUri));
    

    【讨论】:

    • 不,同样的问题,猜猜这与毕加索无关:P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    相关资源
    最近更新 更多