0

in my application i have to load an image with high resolution example(1500*1500). i am using touchimageview library to acheive move,double tap zoom,pinch zoom functions. when i want to load the image from my local resource BitmapFactory.decodeFileDescriptor() throws out of memory exception.

i have searched through web and found i have to sub sample the image to load in image view . but i don't want to sub sample because while zooming the image it looks pixelated. is there any way to load the image without out of memory exception and also it should work for zooming functions.

Dinesh Kannan
  • 1,255
  • 13
  • 32
  • found the answer from this link http://stackoverflow.com/questions/15175760/android-pinch-zoom-large-image-memory-efficient-without-losing-detail?rq=1 – Dinesh Kannan Mar 11 '15 at 13:02

1 Answers1

0

You need to resize the image check this method

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
    int reqWidth, int reqHeight) {

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

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

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, 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;
}
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • thanks for your reply. but i don't want to resize the image because . i am having the zooming functions .if i resized the images it looks pixelated.is there any other ways to acheive this? – Dinesh Kannan Mar 11 '15 at 12:57
  • When you decode any big image it will give out of memory exception because execution memory is very limited in android. If you do not want to resize the image the direct use the resourceid on image view – Sunil Kumar Mar 11 '15 at 13:05
  • thanks again.. see the link https://github.com/davemorrissey/subsampling-scale-image-view – Dinesh Kannan Mar 11 '15 at 13:08
  • yes it looks like this SubsamplingScaleImageView is possible thanks to share it – Sunil Kumar Mar 11 '15 at 13:14