2

I am loading images from URL and showing on the imageview at runtime.

My problem is sometimes my images are shown only HALF OF THE IMAGE and rest part of the image is shown white. Have read many posts on the net but none of them has helped me in the same..

Kindly provide a good solution for the same and the real cause.

Here is the code I have used to create the bitmap.

public static Bitmap getBitmapFromCard(String path, int reqWidth,
            int reqHeight) {
        try {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            BitmapFactory.decodeFile(path, options);
            options.inSampleSize = calculateInSampleSize(options, reqWidth,
                    reqHeight);
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(path, options);
        } catch (OutOfMemoryError e) {
            System.gc();
            System.gc();
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            BitmapFactory.decodeFile(path, options);
            options.inSampleSize = 3;
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(path, options);
        }
    }

Calculation - InSampleSize

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 heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

Screenshot attached.enter image description here

Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143
  • did you check whether you got full image by means of bytes while downloading from URL – Vishwa Aug 17 '15 at 10:17
  • and did you compare the downloaded image bytes with url image content length – Vishwa Aug 17 '15 at 10:20
  • try something else, Instead of decodeFile decodeStream BitmapFactory.decodeStream(ctx.openInputStream(uri), null, o); – Tomer Mor Aug 17 '15 at 10:26
  • if this method doesn't work you could try using the Picasso Library. This library is used to load pictures from url – R. Adang Aug 17 '15 at 10:35
  • @GauravArora Can you please, add more details on the conditions you are using? How many bytes are the images, and what is the device you are using? As far as I could read, your problem seems to be memory related. [Please, check here](http://stackoverflow.com/questions/2414105/why-is-it-bad-practice-to-call-system-gc) about using the System.gc(). – Bonatti Aug 17 '15 at 11:58
  • @GauravArora Is this a JPG image? What version of Android? Is this reproducible in all devices? Also, have you found a fix for this? – Henry Oct 23 '16 at 07:50

0 Answers0