Waqas Ahmed Khan
Waqas Ahmed Khan

Reputation: 353

How to display large size bitmaps in imageview android?

I have images more than 6000px height. Whenever I tried to display these images I got out of memory exception.

I have seen many links but none of matches my need. Because mostly people suggesting image re-sizing solution. But If I'll re-size my image than I am unable to read the text in images due to poor quality.

I want some help in creating some code that can open an image without re-sizing it also with zooming effect.

Any help would be really appreciated.

Upvotes: 1

Views: 3848

Answers (5)

Stanislav
Stanislav

Reputation: 405

Try to use google recommendation for this problem.

For avoid OOM you need to implement this block of code:

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: 4

Rajesh N
Rajesh N

Reputation: 6693

In Application manifest file tag set largeHeap true

 <application
         android:largeHeap="true"

Also Use Picasso or Glide to load image in ImageView

Upvotes: 0

ahmad dehghan
ahmad dehghan

Reputation: 37

try this:

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fullscreen_view);

    imageView = (ImageView) findViewById(R.id.full_image_view);
    imageView.setImageBitmap(decodeSampledBitmapFromResource(imgpath));
}


  //Load a bitmap from a resource with a target size
Bitmap decodeSampledBitmapFromResource(String res) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(res, options);

    //Calculate display dimention for maximum reqwidth and reqheigth 
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int xDim = size.x;
    int yDim = size.y;


    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, xDim, yDim);
    // Decode bitmap with inSampleSize se5t
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(res, options);
}


int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    int inSampleSize = 1; //Default subsampling size
    // Calculate the largest inSampleSize value 
    while ((options.outHeight / inSampleSize) > reqHeight
            || (options.outWidth / inSampleSize) > reqWidth) {
        inSampleSize += 1;
    }
    return inSampleSize;
}

Upvotes: 0

Rakhita Wickramatunge
Rakhita Wickramatunge

Reputation: 4503

Try using a WebView to show the image, instead of ImageView

Upvotes: 3

Alexandre
Alexandre

Reputation: 157

I would suggest you "cut" this image in 3 smaller ones and then load them as three images: the first one will be at y:0 and be height 2000px, the second one y:2000px and height 2000px and the third one at y: 4000px. Do you think this could work? Good luck

Upvotes: 0

Related Questions