0

I'm trying to make an android app with ListView that has an image in each row. I want to store all the images in SQLite database with BLOB data type and then populate the ListView with these images,but whenever I've got more than 2 jpeg images in the list i get OutOfMemoryError. I already tried many techniques found on the internet to reduce the use of memory but still can't solve this problem. I found the use of options.inJustDecodeBounds and options.inSampleSize in android documentation but still getting the same exception. Below you can find my code with Bitmap to byte array and byte array to bitmap conversion.

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;


public class ListItem {

private int id;
private byte [] img;
private String title;
private String description;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public byte[] getImg() {
    return img;
}

public void setImg(byte[] img) {
    this.img = img;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public static byte[] convertBitmapToBytes(Bitmap bmp){
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    return byteArray;
}

public static Bitmap getBitmap(byte [] bitmapdata){
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    ByteArrayInputStream inStream = new ByteArrayInputStream(bitmapdata);
    BitmapFactory.decodeStream(inStream, null, options);
    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, 50, 50);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap bmp = BitmapFactory.decodeStream(inStream,null,options);
    return bmp;
}

public 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) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }
    return inSampleSize;
}

}
2
  • try to set in Manifest.xml <application android:largeHeap="true">... Commented Mar 27, 2016 at 20:02
  • still getting same error Commented Mar 27, 2016 at 20:23

3 Answers 3

1

I've got the same issue in a ListView. I solved it by freeing the allocated memory on leaving the activity. For this I called the method "recycle()" for each displayd bitmap.

private List<Bitmap> m_displayedBitmaps = new ArrayList<Bitmap>();

private List<ImageView> m_displayedGridElements = new ArrayList<ImageView>();

// protected void onPause() {
public void onStop() {

    // don't forget to free the references to your bitmap
    for (ImageView currentGridElement : m_displayedGridElements) {
        currentGridElement.setImageBitmap(null);
    }
    m_displayedGridElements.clear();

    // recycle each image
    for (Bitmap currentBitmap : m_displayedBitmaps) {
        currentBitmap.recycle();
    }
    // clear the list
    m_displayedBitmaps.clear();



    super.onStop();
}
Sign up to request clarification or add additional context in comments.

Comments

0

Android has a very bad image-memory managment. so you need to be very careful handling images. Fortunatelly you can try with an external library to take care of image display. Try with universal image loader UIL

Comments

0

You should not store images in SQLite. Instead use an image loading/caching library like Glide/Picasso, both of them are extremely simple to use and cache images out of the box.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.