gpt4 book ai didi

android - BitmapFactory.Options 返回 0 宽度和高度

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:06:52 24 4
gpt4 key购买 nike

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
final int height = options.outHeight;
final int width = options.outWidth;

path为正确的图片文件路径。

问题是 options.outHeightoptions.outWidthLandscape 中捕获图像时为 0 打开自动旋转 模式。如果我关闭 AutoRotate,它工作正常。因为它的宽度和高度都是 0,所以我在最后得到了一个空的 Bitmap

完整代码:

Bitmap photo = decodeSampledBitmapFromFile(filePath, DESIRED_WIDTH,
DESIRED_HEIGHT);
public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth,
int reqHeight) {

// 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.ARGB_8888;
int inSampleSize = 1;

if (height > reqHeight) {
inSampleSize = Math.round((float) height / (float) reqHeight);
}
int expectedWidth = width / inSampleSize;
if (expectedWidth > reqWidth) {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
options.inSampleSize = inSampleSize;

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

return BitmapFactory.decodeFile(path, options);
}

最佳答案

我有同样的问题,我通过更改解决了它:

BitmapFactory.decodeFile(path, options);

到:

try {
InputStream in = getContentResolver().openInputStream(
Uri.parse(path));
BitmapFactory.decodeStream(in, null, options);
} catch (FileNotFoundException e) {
// do something
}

更改后,宽度和高度设置正确。

关于android - BitmapFactory.Options 返回 0 宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20070219/

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