Android的图像大小调整 [英] Android image resize

查看:136
本文介绍了Android的图像大小调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有存储在SD卡上(每个〜4MB的大小)的图像。

I have images stored on sd card(size of each ~ 4MB).

我要调整每次,比它设置为ImageView的。

I want to resize each, than set it to ImageView.

但是,使用 BitmapFactory.de codeFILE(路径) becouse异常结果我不能这样做
java.lang.OutOfMemoryError 时,会出现。

But I cannot do it using BitmapFactory.decodeFile(path) becouse Exception
java.lang.OutOfMemoryError is appeared.

我如何可以调整图像而不在内存中加载它。是真的吗?

How can i resize image without loading it in the memory. Is it real?

推荐答案

使用位图选项:

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565; //Use this if you dont require Alpha channel
options.inSampleSize = 4; // The higher, the smaller the image size and resolution read in

然后设置在德code选项

Then set the options in the decode

BitmapFactory.decodeFile(path, options)

这里是一个很好的链接通读,有关如何显示位图有效牙缝。

您甚至可以写这样的方法在您需要的分辨率来获得尺寸图像。

You could even write a method like this to obtain a sized image at your desired resolution.

以下方法检查大小的图像,然后去codeS从文件中,样品尺寸从SD卡相应调整你的形象,同时保持内存使用率处于较低。

The following method checks the size the image, then decodes from file, with in-sample size to resize your image from sdcard accordingly, while keeping memory usage at a low.

   public static Bitmap decodeSampledBitmapFromFile(string path,
        int reqWidth, int reqHeight) { // BEST QUALITY MATCH

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

    // Calculate inSampleSize
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        int inSampleSize = 1;

        if (height > reqHeight) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        }

        int expectedWidth = width / inSampleSize;

        if (expectedWidth > reqWidth) {
            //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }


    options.inSampleSize = inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(path, options);
  }

这篇关于Android的图像大小调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆