0

I have an ImageView and I need to getImageResource() based on user GPS position. There are 6 images and as the distance between 2 points decrease I replace the image with a new resource.

I'm testing the app on the Galaxy S4 and the problem is that after a very small random number of loading the app crashes because of OutOfMemory.

Is there a good way to cache the images? (Maybe I need to load them by using an AsyncTask)

The images are 400x400px png- 24 bit with transparency.

Thank you

2
  • Which of the drawable-x folders contain your images? Commented Jul 11, 2013 at 18:31
  • I placed everything in mdpi folder just for now. Commented Jul 11, 2013 at 18:52

2 Answers 2

1

Try using this:

public static Bitmap decodeSampledBitmapFromResource(String uri,
        int reqWidth, int reqHeight, int orientation) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(uri, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap decodeFile = BitmapFactory.decodeFile(uri, options);
    int rotate = 0;
    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_270:
        rotate = 270;
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotate = 180;
        break;
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotate = 90;
        break;
    }
    Matrix matrix = new Matrix();

    // matrix.postScale(scaleWidth, scaleHeight);
    matrix.postRotate(rotate);

    Bitmap rotatedBitmap = Bitmap.createBitmap(decodeFile, 0, 0,
            decodeFile.getWidth(), decodeFile.getHeight(), matrix, true);

    return rotatedBitmap;

}

private static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Galaxy S4 most likely works with the xxhdpi drawables, so you putting everything in mdpi will have the system scale up your images to match S4's dpi level, hence the OutOfMemory error. Try scaling and placing drawables in their respective folders depending on the dpi (including xhdpi and xxhdpi) and after that maybe optimize your code.

1 Comment

I've tried very quickly and it seems you are right. The image replacement is smoother and the heap grows slower (size is halved). I'll try better tomorrow. Thanks for now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.