gpt4 book ai didi

android - 将位图保存为 jpeg 图像

转载 作者:行者123 更新时间:2023-11-29 19:44:01 25 4
gpt4 key购买 nike

在我的 android 应用程序中,我生成了一个二维码,然后将其保存为 jpeg 图像,我使用以下代码:

imageView = (ImageView) findViewById(R.id.iv);
final Bitmap bitmap = getIntent().getParcelableExtra("pic");
imageView.setImageBitmap(bitmap);
save = (Button) findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


String path = Environment.getExternalStorageDirectory().toString();

OutputStream fOutputStream = null;
File file = new File(path + "/Captures/", "screen.jpg");
if (!file.exists()) {
file.mkdirs();
}

try {

fOutputStream = new FileOutputStream(file);

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);

fOutputStream.flush();
fOutputStream.close();
MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
}
}
});

但它总是在以下行捕获异常:

fOutputStream = new FileOutputStream(file);

是什么导致了这个问题???

最佳答案

what caused this problem???

语句 file.mkdirs(); 创建了一个名为 screen.jpg 的目录。 FileOutputStream 无法创建名称为 screen.jpg 的文件,但已找到具有该名称的目录。所以你得到了:

java.io.FileNotFoundException

能否请您替换以下 fragment :

File file = new File(path + "/Captures/", "screen.jpg");
if (!file.exists()) {
file.mkdirs();
}

通过以下 fragment :

String dirPath = path + "/Captures/";       
File dirFile = new File(dirPath);
if(!dirFile.exists()){
dirFile.mkdirs();
}
File file = new File(dirFile, "screen.jpg");

并查看结果?

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

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