Android的BitmapFactory

Android的BitmapFactory

本文介绍了Android的BitmapFactory.Options.inSampleSize casuse应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们是在一个Android应用程序捕捉图像和转换同样为位图被张贴到REST服务工作。

在创建我们BitmapFactory.Options.isSample大小配置为4个价值,这正常工作的所有设备上,除了来回的HTC One位图。在HTC一项所述的图像distroted

当我们把值改为1就显示了HTC一个良好的形象,但所有其他设备上的应用程序崩溃,因为内存问题。

的问题与堆大小可用。


  1. 什么是原因的HTC One显示了HTC一个扭曲的图像。

  2. 为什么应用程序崩溃的S3其中也有相同的构建
    16但版本崩溃由于堆大小不可用。

  3. 为什么所有低版本手机一样的机器人工作正常样本
    4个尺寸。

  4. 最新最好的5月至检查可用内存空间为应用和
    使用可用的位图工作。

当我们配置的android:largeHeap清单文件这似乎工作,但它并不似乎是一个可靠的解决方案。


我能够找到使用照相机参数拍摄的图像的大小,然后在位的最佳样本大小到达。下面code解决问题。但是,它这样做的正确的方式或者我应该需要计算样本量太大,而不是与1和4下去。

 私人位图createImaegBitmap(字节[]数据)
    {
        BitmapFactory.Options选择;        选择=新BitmapFactory.Options();
        opt.inTempStorage =新的字节[16 * 1024];
        参数参数=((ReceiptCollection)getApplication())getParams()方法。
        尺寸尺寸= parameters.getPictureSize();        INT height11 = size.height;
        INT width11 = size.width;
        浮动MB =(width11 * height11)/ 1024000;        如果(MB> 4F)
            opt.inSampleSize = 4;
        否则,如果(MB> 3F)
            opt.inSampleSize = 1;
        位图位图= BitmapFactory.de codeByteArray的(数据,0,data.length,OPT);
        返回位图;
    }


解决方案

看:结果

1:因为......直接向DOC:如果设置的值> 1,要求德codeR二次采样原始图像,返回较小的图像以节省内存。样本大小是对应于德$ C $光盘图中单个像素中任一维的像素数。例如,inSampleSize == 4返回一个图像是1/4原稿的宽度/高度,和1/16的像素数。任何值< = 1的处理方式相同。注意:去codeR使用基于2的幂最终值,任何其他值将四舍五入为2的最近的电源

2:我的猜测是,因为不是所有的设备都具有相同的可用内存。
3:你需要计算样本量不只是设置了一些...

2,3:所有的设备有不同的摄像头的分辨率。

在这个问题上的工作阅读:

 公共静态位图lessResolution(字符串文件路径){
    INT reqHeight = 120;
    INT reqWidth = 120;
    BitmapFactory.Options选项=新BitmapFactory.Options();    //首先去code。与inJustDe codeBounds = true来检查尺寸
    options.inJustDe codeBounds = TRUE;
    BitmapFactory.de codeFILE(文件路径,期权);    //计算inSampleSize
    options.inSampleSize = calculateInSampleSize(选项,reqWidth,
            reqHeight);    //德code位与inSampleSize集
    options.inJustDe codeBounds = FALSE;    返回BitmapFactory.de codeFILE(文件路径,期权);
}私有静态诠释calculateInSampleSize(BitmapFactory.Options选项,
        INT reqWidth,诠释reqHeight){    最终诠释身高= options.outHeight;
    最终诠释宽度= options.outWidth;
    INT inSampleSize = 1;    如果(高度> reqHeight ||宽度GT; reqWidth){        //计算高度和宽度的比值要求的高度和
        //宽
        最终诠释heightRatio = Math.round((浮点)高度
                /(浮点)reqHeight);
        最终诠释widthRatio = Math.round((浮点)宽/(浮点)reqWidth);        //选择最小比率inSampleSize价值,这将
        //保证
        //使用两个维度大于或等于所述最终图像
        //请求的高度和宽度。
        inSampleSize = heightRatio< widthRatio? heightRatio:widthRatio;
    }
    返回inSampleSize;
}

We are working on a Android app that captures images and converts the same to Bitmap to be posted to a Rest service.

While creating the Bitmap we have BitmapFactory.Options.isSample Size configured to value of 4 and this works fine on all device except fro HTC One. The image in HTC one is distroted.

When we change the value to 1 it shows a good image on HTC one but the application crashes on all other device because of Memory issues.

The issue is related to Heap size available.

  1. What is the reason that HTC one shows a distorted image on HTC one.
  2. Why is application crashing on S3 which also has the same buildversion of 16 but crashes due to heap size unavailability.
  3. Why does all lower version phone like droid works fine for samplesize of 4.
  4. Whats the best may to check available memory space for an app anduse the available for working with BITMAP.

When we configure android:largeHeap in manifest file this seems to work but it does not seems to be a dependable solution.


I was able to find the size of the picture taken using camera parameters and then arrive at optimal sample size of bitmap. The below code solves issue. But is it right way of doing or should i need to compute sample size too instead of going with 1 and 4.

private Bitmap createImaegBitmap(byte[] data)
    {
        BitmapFactory.Options opt;

        opt = new BitmapFactory.Options();
        opt.inTempStorage = new byte[16 * 1024];
        Parameters parameters = ((ReceiptCollection)getApplication()).getParams();
        Size size = parameters.getPictureSize();

        int height11 = size.height;
        int width11 = size.width;
        float mb = (width11 * height11) / 1024000;

        if (mb > 4f)
            opt.inSampleSize = 4;
        else if (mb > 3f)
            opt.inSampleSize = 1;
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,opt);
        return bitmap;
    }
解决方案

look:
http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html

1: because... Direct FROM DOC: If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: the decoder uses a final value based on powers of 2, any other value will be rounded down to the nearest power of 2.

2:My guess is because not all the devices have the same memory available.3: you need to calculate the sample size no just set a number...

2,3: all devices have different cameras resolution.

to work on this issue read this: http://developer.android.com/training/displaying-bitmaps/index.html

    public static Bitmap lessResolution(String filePath) {
    int reqHeight = 120;
    int reqWidth = 120;
    BitmapFactory.Options options = new BitmapFactory.Options();

    // First decode with inJustDecodeBounds=true to check dimensions
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);

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

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

    return BitmapFactory.decodeFile(filePath, options);
}

private static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {

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

这篇关于Android的BitmapFactory.Options.inSampleSize casuse应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 01:16