Home » Android » android – Picasso fails to load large images (from Camera and local Uri)

android – Picasso fails to load large images (from Camera and local Uri)

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a problem using Picasso trying to load large images from a local Uri of format content://com.android.providers.media.documents/document/imageXXYYZZ both from Gallery and Camera Intent.

I am loading the images with a standard call:

Picasso.load(image_url)
        .resize(600, 240)
        .centerCrop()
        .into(imageTarget);

I attached here a Target and when I am getting the onBitmapFailed(Drawable errorDrawable) error triggered. Also, when I log Picasso I get:

06-23 12:13:54.267  22393-22393/it.b3lab.friendipity D/Picasso﹕ Main        created      [R100] Request{content://com.android.providers.media.documents/document/image%3A13345 resize(600,240) centerCrop}
06-23 12:13:54.277  22393-23010/it.b3lab.friendipity D/Picasso﹕ Dispatcher  enqueued     [R100]+9ms
06-23 12:13:54.285  22393-23038/it.b3lab.friendipity D/Picasso﹕ Hunter      executing    [R100]+15ms
06-23 12:13:54.813  22393-23010/it.b3lab.friendipity D/Picasso﹕ Dispatcher  batched      [R100]+546ms for error
06-23 12:13:55.014  22393-23010/it.b3lab.friendipity D/Picasso﹕ Dispatcher  delivered    [R100]+746ms
06-23 12:13:55.024  22393-22393/it.b3lab.friendipity I/picasso﹕ failed to load bitmap
06-23 12:13:55.024  22393-22393/it.b3lab.friendipity D/Picasso﹕ Main        errored      [R100]+756ms

This only happens as I said above when I try to load large images from the gallery (above around 1 MB) and from the Camera intent when using a hi-res camera smartphone (in my case it’s a Moto G running on Android 5.0.1). I am not getting this error using a Samsung S2 on Android 4.4.

Any help would be really appreciated! Thanks

How to&Answers:

You need to resolve the content uri to an absolut uri. Eg like this:

public String getAbsolutePathFromURI( Uri contentUri ) {
  String[] proj = { MediaStore.Images.Media.DATA };
  Cursor cursor = activity.getContentResolver().query( contentUri, proj, null, null, null );
  int columnIndex = cursor.getColumnIndexOrThrow( MediaStore.Images.Media.DATA );
  cursor.moveToFirst();
  String path = cursor.getString( columnIndex );
  cursor.close();
  return path;
}

Answer:

I spent a lot of time figuring out how to load a large image into image view using Picasso. It was working on genymotion but wasn’t working on my moto x. So I finally decided to manually resize the image inside an async task and then load it into image view without using Picasso.

new AsyncTask<String, Void, Void>() {
                @Override
                protected Void doInBackground(String... params) {
                    String path = params[0];
                    final Bitmap resizedBitmap = ImageUtils.getResizedBitmap(200, 200, PATH_TO_IMAGE);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            imageView.setImageBitmap(resizedBitmap);
                        }
                    });
                    return null;
                }
            }.execute(imageLoadPath);

You can find ImageUtils.getResizedBitmap() method here

Answer:

Picasso fails but this works perfect

   public static void resizeImage(String file, int maxTargetWidth, int maxTargetHeight) {
    try {
        InputStream in = new FileInputStream(file);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        close(in);

        int inWidth = options.outWidth;
        int inHeight = options.outHeight;

        in = new FileInputStream(file);
        options = new BitmapFactory.Options();
        options.inSampleSize = Math.max(inWidth / maxTargetWidth, inHeight / maxTargetHeight);
        Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options);

        Matrix m = new Matrix();
        RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight());
        RectF outRect = new RectF(0, 0, maxTargetWidth, maxTargetHeight);
        m.setRectToRect(inRect, outRect, CENTER);
        float[] values = new float[9];
        m.getValues(values);

        Bitmap resizedBitmap = createScaledBitmap(roughBitmap,
                (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]),
                true);

        resizedBitmap = rotateBitmap(file, resizedBitmap);
        FileOutputStream out = new FileOutputStream(file);
        resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
        close(out);
    } catch (Exception e) {
        error(e);
    }
}

private static Bitmap rotateBitmap(String src, Bitmap bitmap) {
    try {
        int orientation = new ExifInterface(src).getAttributeInt(TAG_ORIENTATION, 1);
        Matrix matrix = new Matrix();
        if (orientation == ORIENTATION_NORMAL) return bitmap;
        if (orientation == ORIENTATION_FLIP_HORIZONTAL) matrix.setScale(-1, 1);
        else if (orientation == ORIENTATION_ROTATE_180) matrix.setRotate(180);
        else if (orientation == ORIENTATION_FLIP_VERTICAL) {
            matrix.setRotate(180);
            matrix.postScale(-1, 1);
        } else if (orientation == ORIENTATION_TRANSPOSE) {
            matrix.setRotate(90);
            matrix.postScale(-1, 1);
        } else if (orientation == ORIENTATION_ROTATE_90) {
            matrix.setRotate(90);
        } else if (orientation == ORIENTATION_TRANSVERSE) {
            matrix.setRotate(-90);
            matrix.postScale(-1, 1);
        } else if (orientation == ORIENTATION_ROTATE_270) {
            matrix.setRotate(-90);
        } else return bitmap;
        try {
            Bitmap oriented = createBitmap(bitmap, 0, 0,
                    bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();
            return oriented;
        } catch (OutOfMemoryError e) {
            error(e);
        }
    } catch (IOException e) {
        error(e);
    }
    return bitmap;
}

Answer:

Try this:

Uri uri = Uri.parse(imageUri);
Picasso.with(context)
    .load(uri)
    .fit()
    .resize(600, 240)
    .skipMemoryCache()
    .transform(new DocumentExifTransformation(this, uri))
    .into(imageView);

If i doesn’t work take a look here https://github.com/square/picasso/issues/539