如何在imageview android中显示大尺寸位图?

时间:2014-07-17 05:49:42

标签: android bitmap imageview out-of-memory

我的图像高度超过6000像素。每当我试图显示这些图像时,我都会遇到内存异常。

我见过许多链接,但没有一个符合我的需要。因为大多数人建议图像重新调整大小的解决方案。但是如果我重新调整图像尺寸,那么由于质量差,我无法读取图像中的文字。

我想要一些帮助来创建一些可以打开图像的代码,而无需通过缩放效果重新调整图像大小。

任何帮助都会非常感激。

5 个答案:

答案 0 :(得分:4)

尝试针对此问题使用google建议。

为避免OOM,您需要实现此代码块:

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

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

答案 1 :(得分:3)

尝试使用WebView显示图像,而不是ImageView

答案 2 :(得分:0)

我建议你用3个较小的图像“剪切”这个图像然后将它们加载为三个图像:第一个将在y:0并且高度为2000px,第二个y:2000px和高度2000px和第三个一个在y:4000px。 你认为这可行吗? 祝你好运

答案 3 :(得分:0)

试试这个:

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

答案 4 :(得分:0)

在应用程序清单文件标记中设置largeHeap true

 <application
         android:largeHeap="true"

还可以使用Picasso或Glide在ImageView中加载图像