Picasso not showing Images from gallery on certain

2019-08-16 00:11发布

On some (all?) Samsung devices (Galaxy S5, S3) Picasso does not display Images from the Gallery.

This is the code:

File[] picFiles = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).listFiles();

File file = picFile[0]; 

I tried:

Picasso.with(getActivity()).load(Uri.fromFile(file)).into(imageView);

as well as

Picasso.with(getActivity()).load("file://" + file.getAbsolutePath()).into(imageView);

to no avail. On all non-samsung devices boths snippets work fine:

The path to the files do not look different from other devices, example:

/storage/emulated/0/Pictures/JPEG_20150706_112313_826588781.jpg

What could be the problem here?

EDIT: Yes I have added android.permission.READ_EXTERNAL_STORAGE. The file exists and the Image is visible in the gallery app.

EDIT 2: This is the stracktrace from Picasso:

07-13 07:22:22.817  14648-14921/xy I/dalvikvm﹕ "Picasso-/storage/emulated/0/Pictures/JPEG_20150713_071858_-1853613631.jpg" prio=5 tid=51 RUNNABLE
07-13 07:22:22.817  14648-14921/xy I/dalvikvm﹕ at com.squareup.picasso.BitmapHunter.decodeStream(BitmapHunter.java:142)
07-13 07:22:22.817  14648-14921/xy I/dalvikvm﹕ at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:217)
07-13 07:22:22.817  14648-14921/xy I/dalvikvm﹕ at com.squareup.picasso.BitmapHunter.run(BitmapHunter.java:159)
07-13 07:22:22.817  14648-14921/xy I/dalvikvm﹕ at com.squareup.picasso.Utils$PicassoThread.run(Utils.java:411)
07-13 07:22:23.167  14648-14918/xy﹕ "Picasso-/storage/emulated/0/Pictures/JPEG_20150713_071858_-1853613631.jpg" prio=5 tid=49 RUNNABLE
07-13 07:22:23.167  14648-14918/xy﹕ at com.squareup.picasso.BitmapHunter.transformResult(BitmapHunter.java:558)
07-13 07:22:23.167  14648-14918/xy﹕ at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:232)
07-13 07:22:23.167  14648-14918/xy﹕ at com.squareup.picasso.BitmapHunter.run(BitmapHunter.java:159)
07-13 07:22:23.167  14648-14918/xy﹕ at com.squareup.picasso.Utils$PicassoThread.run(Utils.java:411)

It doesn't really seem to reveal the reason for the image not being displayed.

Picasso version is 2.5.2, I could reproduce the error on a Samsung S3 with Android 4.4.4 and a Samsung S5 with Android 5.0.

UIL is loading the same image just fine, so it's probably a bug in Picasso, I'll probably file a bug report at their github.

LAST EDIT: The problem actually was an OOM exception that eluded my attention because I filtered my LogCat for the wrong keywords. I'm quite ashamed of myself now.

picasso.load().fit().into() 

works and the image is being displayed. Thanks for your help.

1条回答
做自己的国王
2楼-- · 2019-08-16 00:54

You are most likely loading very large images which can lead to an OutOfMemoryError. You can resize the Bitmap and keep the aspect ratio.

Example of getting the width and height of a bitmap without loading it into memory:

File file = new File("/path/to/some/picture.jpg");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
int width = options.outWidth;
int height = options.outHeight;

Tell Picasso to resize the image to avoid an OutOfMemoryError:

if (width > MAX_WIDTH || height > MAX_HEIGHT) {
    Picasso.with(context).load(uri)
        .resize(MAX_WIDTH, MAX_HEIGHT)
        .centerInside()
        .into(yourImageView);
}
查看更多
登录 后发表回答