1

This problem doesn't occur on my phone because it has a ram of 2 gb but there are a lot of mobiles that have a ram of 1 gb and this problem occurs almost every time on such a mobile . Every time image in loaded from gallery and placed on an imageview it crash with an outofmemory error.

This is my code . I get the width height of the image and then scale the image but still doesn't work. I also use picasso because it works asynchronously.

public static void setGalleryImageWithPicasso(Activity context, Uri imageURI, int image, final PicassoBitmapCallBack callBack) {
    int MAX_WIDTH =500;
    int MAX_HEIGHT =500;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    String filePath = getPath(imageURI, context);
    BitmapFactory.decodeFile(filePath, options);
    int width = options.outWidth;
    int height = options.outHeight;

    while (width > MAX_WIDTH && height > MAX_HEIGHT) {
        width = (width * 2) / 3;
        height = (height * 2) / 3;
    }
        Picasso.with(context)
                .load(imageURI)
                .resize(width, height)
                .centerInside()
                .placeholder(image)
                .error(image)
                .into(new Target() {

                    @Override
                    public void onBitmapLoaded (Bitmap bmp, Picasso.LoadedFrom from){  
                        // Place image on imageview etc
                        callBack.onSuccess(bmp);

                    }

                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {

                    }
                });
}

public static String getPath(Uri uri, Activity activity) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = activity.getContentResolver().query(uri, projection, null, null, null);
    int column_index = 0;
    if (cursor != null) {
        column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
    }
    int columnIndex = 0;
    if (cursor != null) {
        columnIndex = cursor.getColumnIndex(projection[0]);
    }
    String filePath = null;
    if (cursor != null) {
        filePath = cursor.getString(columnIndex);
        cursor.close();
    }
    return filePath;
}

I looked into insample size and checked a lot but wasn't able to understand it so plz can someone post modifications to my code and not give me links . And i don't understand 500 * 500 is pretty small but i still get the same error.

Syed Muhammad Oan
  • 687
  • 2
  • 15
  • 39

0 Answers0