0

Steps my application performs:-

  1. Download a large no of images and save them on the SDCard.
  2. Load every image into Bitmap and resize them, after resizing replace this resized image with the original one.

My code:-

Bitmap myimage = loadimage(path+temppos+".jpg");
Bitmap finalimage = getResizedBitmap(myimage,width,height);
//save image 
.....
//recyclebitmap 
myimage.recycle();
finalimage.recycle();

loadimage:-

public Bitmap loadimage(String path)
    {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inDither=true;
        options.inPurgeable=true;
        options.inInputShareable=true;
        return BitmapFactory.decodeFile(path, options);
    }

Now I' populating these images on gridview.

Output(Before):- enter image description here

Output (After):- enter image description here

Where Before corresponds to initially when only a few images are downloaded. And After corresponds to after all the images are downloaded.

Now, I think it is happening maybe because of Bitmap.recycle() method but don't know the reason. Please correct me if I am wrong and point out the error here.

Edit: I must add the grid view shows around 50 downloaded images, but only the first three images are becoming unrecognizable.

Thanks.

Mohit
  • 1,045
  • 4
  • 18
  • 45
  • would you please try `options.inSampleSize=3` before `decodeFile`. maybe you have memory problem. – Hana Bzh Dec 04 '14 at 07:39
  • JPEG is a **LOSSY** compression format. Each time you resave an image, it **looses details**. Also this: `options.inPreferredConfig = Bitmap.Config.RGB_565;` **lowers the available colors** (16 bit). – Phantômaxx Dec 04 '14 at 08:01
  • @HBizhi Tried it. Getting the same problem. I checked the downloaded images, and the first 3 images are unrecognizable. They are saved as they are shown in the after output. – Mohit Dec 04 '14 at 08:04
  • @Der Golem yeah bit I am performing the same operations on all the images then why only the first three images are distorting. – Mohit Dec 04 '14 at 08:06
  • MAybe the other ones are **better quality** (lower compression) in the beginning. The ones distorting might be **lower quality** (higher compression). Try to temporarily rename the non distorting ones to the names of the distorting ones, to see that they won't be distorted. – Phantômaxx Dec 04 '14 at 08:14
  • I will try it. But I have 5 list of 50 image urls and whenever I change the urls, only the first 2-3 images are showing this problem, can't be a coincidence that only the first few images are always of lower quality. – Mohit Dec 04 '14 at 08:22

1 Answers1

0

For getting your resized Bitmap you can use the below code: (taken from this tutorial)

public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
            int reqHeight) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

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

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

    public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {

        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;
        }

You can also take a look at the official site for getting a hint on loading Bitmaps

Edit

Try to change bitmap configuration to ARGB_8888 for best quality

because RGB_565 configuration can produce slight visual artifacts depending on the configuration of the source (taken from docs)

Update

I think you can take a look at this answer , I think this would solve your problem

Community
  • 1
  • 1
Shubhang Malviya
  • 1,525
  • 11
  • 17
  • My code for resizing is working perfectly. My problem is with the image distortion happening to the first few downloaded images. – Mohit Dec 04 '14 at 08:50
  • Tried it, no changes. I think the problem is due to improper handling of `bitmap.recycle()` method – Mohit Dec 04 '14 at 09:35
  • can you add your save bitmap function? – Shubhang Malviya Dec 04 '14 at 09:54
  • @Mohit Meanwhile I've updated my answer ,plz take a look – Shubhang Malviya Dec 04 '14 at 10:00
  • I am using download manager. So it just consists of a few lines. `request.setDestinationInExternalFilesDir(....)` after initializing `DownloadManager` and `Request` – Mohit Dec 04 '14 at 10:01
  • Nope. I am already using all the methods the questions does, in my application I am downloading a total of 50 images but only the first few images are distorting. – Mohit Dec 04 '14 at 10:43
  • Have you tried to remove these options.inPurgeable=true; options.inInputShareable=true; and test? – Shubhang Malviya Dec 04 '14 at 11:01
  • I fixed the problem. It was because I was resizing the image to a lower resolution then they were. But why the only first few were distorting, I am still unable to understand. – Mohit Dec 04 '14 at 14:50