Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android BitmapFactory.Options.inSampleSize CAUSE应用程序崩溃_Android_Heap Memory_Bitmapimage - Fatal编程技术网

Android BitmapFactory.Options.inSampleSize CAUSE应用程序崩溃

Android BitmapFactory.Options.inSampleSize CAUSE应用程序崩溃,android,heap-memory,bitmapimage,Android,Heap Memory,Bitmapimage,我们正在开发一款Android应用程序,它可以捕获图像并将图像转换为位图,然后发布到Rest服务 创建位图时,我们将BitmapFactory.Options.isSample Size配置为值4,这在除HTC One之外的所有设备上都可以正常工作。HTC one中的图像失真 当我们将该值更改为1时,它在HTC one上显示良好的图像,但由于内存问题,应用程序在所有其他设备上崩溃 这个问题与可用的堆大小有关 HTC one在HTC one上显示扭曲图像的原因是什么 为什么应用程序在同样具有相同构

我们正在开发一款Android应用程序,它可以捕获图像并将图像转换为位图,然后发布到Rest服务

创建位图时,我们将BitmapFactory.Options.isSample Size配置为值4,这在除HTC One之外的所有设备上都可以正常工作。HTC one中的图像失真

当我们将该值更改为1时,它在HTC one上显示良好的图像,但由于内存问题,应用程序在所有其他设备上崩溃

这个问题与可用的堆大小有关

  • HTC one在HTC one上显示扭曲图像的原因是什么
  • 为什么应用程序在同样具有相同构建的S3上崩溃 版本16,但由于堆大小不可用而崩溃
  • 为什么所有像droid这样的低版本手机都适合样本 大小为4
  • 检查应用程序可用内存空间的最佳方法是什么 使用可用于处理位图的
  • 当我们在清单文件中配置android:largeHeap时,这似乎可行,但似乎不是一个可靠的解决方案


    我能够找到使用相机参数拍摄的图片的大小,然后得到位图的最佳样本大小。下面的代码解决了这个问题。但这是正确的方法吗?或者我也需要计算样本量,而不是使用1和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;
        }
    
    看:

    1:因为。。。直接从文档:如果设置为值>1,则请求解码器对原始图像进行二次采样,返回较小的图像以节省内存。样本大小是任一维度中对应于解码位图中单个像素的像素数。例如,inSampleSize==4返回的图像的宽度/高度为原始图像的1/4,像素数为1/16。任意值reqHeight | | width>reqWidth){ //计算高度和宽度与所需高度和宽度的比率 //宽度 最终整数高度比=数学圆((浮动)高度 /(浮动)高度); 最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度); //选择最小比率作为采样值,这将 //保证 //最终图像的两个尺寸均大于或等于 //要求的高度和宽度。 inSampleSize=高度比<宽度比?高度比:宽度比; } 返回样本大小; }
    HTC one上生成的图像在设备本身上看起来失真(可能是布局问题)或者实际生成的jpg/png图像在使用某种查看器查看时被扭曲?我们可以在浏览器上查看该服务发布的图像,它与我在应用程序预览中看到的图像一样糟糕。感谢您的回复,我已更新了问题中的代码,但正如您提到的,我正在使用相机参数查找样本大小,但仍在选择介于1和4之间,还是完全使用您的解决方案更好。取决于您的需要,但请记住,在处理高分辨率图像时,使用insample size可避免内存不足。。。有时图像的分辨率太大,将图像分辨率降低到1/4是不够的。不是一种正确的方法来分隔insample size可以具有的值..Gotchu。我会根据你的算法计算样本量。
        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;
    }