Lack of memory with multiple images [closed] Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Lack of memory with multiple images [closed] without wasting too much if your time.

The question is published on by Tutorial Guruji team.

In my application I have to manage lot of images taken by the Android Camera.

The problem is that when I have a lot of pictures the phone has a lack of memory and works very slowly. I want to still be able to manage the same number of images but without the memory issues

Any advice on what I should do to achieve this?


You should decode a scaled images.
You can reduce the amount of dynamic heap used by expanding the JPEG into a memory array that’s already scaled to match the size of the destination view.
The following example method demonstrates this technique:

private void setPic() {
    // Get the dimensions of the View
    int targetW = mImageView.getWidth();
    int targetH = mImageView.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

Read more here.

We are here to answer your question about Lack of memory with multiple images [closed] - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji