gpt4 book ai didi

android - 将位图保存为 jpeg 文件

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

这道题很简单,我们可以:

public static void writePhotoJpg(Bitmap data, String pathName) {
File file = new File(pathName);
try {
file.createNewFile();
// BufferedOutputStream os = new BufferedOutputStream(
// new FileOutputStream(file));

FileOutputStream os = new FileOutputStream(file);
data.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();


} catch (Exception e) {
e.printStackTrace();
}
}

但此方法并非全部成功:因为“注意:并非所有格式都直接支持所有位图配置,因此 BitmapFactory 返回的位图可能处于不同的位深度,并且/或者可能丢失了每像素 alpha (例如 JPEG 仅支持不透明像素)。”在谷歌文档中。不幸的是我遇到了这个问题:在我使用的相机预览中:

 private Bitmap  printScreen() {
View view = this.getWindow().getDecorView();
// if (view.isDrawingCacheEnabled()) {
view.setDrawingCacheEnabled(true);
Calendar c = Calendar.getInstance();
String date = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + " " + c.get(Calendar.HOUR_OF_DAY) + "-" + c.get(Calendar.MINUTE) + "-" + c.get(Calendar.SECOND);
// }
view.buildDrawingCache();

Bitmap bmp = view.getDrawingCache();

return bmp ;
}

所以当我使用 setImageBitmap(bmp) 时,看起来很好,但是当我打开保存的 jpeg 文件时,它是一个黑色的 jpeg。所以我认为保存方法不好,你能告诉我另一种保存方法吗?

最佳答案

你可以试试这个:

 public static final int BUFFER_SIZE = 1024 * 8;
static void writeExternalToCache(Bitmap bitmap, File file) {
try {
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
final BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER_SIZE);
bitmap.compress(CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {

}

}

它在我的代码中有效,所以如果它无效,请尝试查找错误:

1.位图显示对吗?

2.位图保存文件在哪里?该文件有一些限制?喜欢大小或...

如果错误是您发布的原因,您可以尝试使用 Bitmap.Config.RGB_565 再次解码位图。

关于android - 将位图保存为 jpeg 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8800349/

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