'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.
Solution 1:[1]
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;
}
Solution 2:[2]
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
Solution 3:[3]
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;
}
Solution 4:[4]
In Application manifest file tag set largeHeap true
<application
android:largeHeap="true"
Also Use Picasso or Glide to load image in ImageView
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SMR |
| Solution 2 | Alexandre |
| Solution 3 | |
| Solution 4 | Rajesh Nasit |
