Ahsan Kamal
Ahsan Kamal

Reputation: 1105

High resolution image not display in ImageView Android

I am displaying an image in full screen mode,but whenever going to display large image then nothing display in the image view. Below code try to resize bitmap but got same result blank imageview.

public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);
    // RECREATE THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
            matrix, false);
    return resizedBitmap;
}

Upvotes: 2

Views: 6476

Answers (5)

Sarthak Majithia
Sarthak Majithia

Reputation: 301

I had the same problem. Got is solved through the following code. Provide the filepath, desired width and desired height to the method. For more reference see here

public static Bitmap decodeScaledBitmapFromSdCard(String filePath, int reqWidth,
        int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);

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

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

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) {
        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

Upvotes: 2

Syeda Zunaira
Syeda Zunaira

Reputation: 5207

You solve your problem from two mehtods Method 1

call this method(function)

public Bitmap decodeImage(int resourceId) {
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), resourceId, o);
        // The new size we want to scale to
        final int REQUIRED_SIZE = 100; // you are free to modify size as your requirement

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
            scale *= 2;

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeResource(getResources(), resourceId, o2);
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return null;    
}

add this before your adapter

picture.setImageBitmap((decodeImage(item.drawableId));

instead of

 picture.setImageResource(item.drawableId);

Mehtod 2

You need to adjust your image size. The better way is to decode the image to a bitmap, and set the bitmap to the ImageView. For example:

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), item.drawableId, opts);
picture.setImageBitmap (bitmap);

Upvotes: 4

Karan Chunara
Karan Chunara

Reputation: 546

You solve your problem in Fragment Add method in adapter

 public Bitmap decodeImage(int gridViewImageId) {
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(mContext.getResources(), gridViewImageId, o);
        // The new size we want to scale to
        final int REQUIRED_SIZE = 100; // you are free to modify size as your requirement

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
            scale *= 2;

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeResource(mContext.getResources(), gridViewImageId, o2);
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return null;

}

and add this line

imageViewAndroid.setImageBitmap(decodeImage(gridViewImageId[i]));

Upvotes: 0

user4050065
user4050065

Reputation:

may be this will work for you

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

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

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

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

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

Upvotes: 1

Mina Wissa
Mina Wissa

Reputation: 10971

Probably the image size is too large that the system cannot allocate enough memory to load it.

you can try loading your image after scaling it it with BitmapFactory.Options sampleSize

check this link for more info http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Upvotes: 1

Related Questions