gpt4 book ai didi

Android BitmapFactory.Options.inSampleSize 导致应用程序崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 08:57:01 29 4
gpt4 key购买 nike

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

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

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

此问题与可用堆大小有关。

  1. HTC one 在 HTC one 上显示失真图像的原因是什么。
  2. 为什么应用程序在同样具有相同构建的 S3 上崩溃16 的版本,但由于堆大小不可用而崩溃。
  3. 为什么像 droid 这样的所有低版本手机都可以正常工作大小为 4。
  4. 检查应用可用内存空间的最佳方法是什么?使用可用的 BITMAP。

当我们在 list 文件中配置 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;
}

最佳答案

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

1:因为... Direct FROM DOC:如果设置值 > 1,请求解码器对原始图像进行子采样,返回较小的图像以节省内存。样本大小是与解码位图中的单个像素相对应的任一维度中的像素数。例如,inSampleSize == 4 返回的图像是原始宽度/高度的 1/4,像素数的 1/16。任何 <= 1 的值都被视为与 1 相同。注意:解码器使用基于 2 的幂的最终值,任何其他值将向下舍入为最接近的 2 的幂。

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

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

要解决此问题,请阅读: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 导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18243095/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com