gpt4 book ai didi

android - 将 ImageView 保存到特定文件夹中的图像

转载 作者:行者123 更新时间:2023-11-30 03:32:48 24 4
gpt4 key购买 nike

我想将 ImageView 保存到特定文件夹中的图像,我尝试了这段代码,但它不起作用

public void saveImage(View v){  
View content = findViewById(R.id.iv_photo);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
//File file = new File("/DCIM/Camera/image.jpg");
File root = Environment.getExternalStorageDirectory();
File file = new File(root.getAbsolutePath() + "/DCIM/image.jpg");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
}catch (Exception e){
e.printStackTrace();
}

}

以及调用函数的代码

saveImage(getWindow().getDecorView().findViewById(android.R.id.content));

最佳答案

要从应用的资源中保存图像文件,您可以这样进行:

File dest = Environment.getExternalStorageDirectory();
InputStream in = context.getResources().getDrawable(R.drawable.my_image);
// Used the File-constructor
OutputStream out = new FileOutputStream(new File(dest, "myNewImage.png"));

// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
try {
// A little more explicit
while ( (len = in.read(buf, 0, buf.length)) != -1){
out.write(buf, 0, len);
}
} finally {
// Ensure the Streams are closed:
in.close();
out.close();
}

这应该可以解决问题。

关于android - 将 ImageView 保存到特定文件夹中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17166317/

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