Android 将imageview保存到特定文件夹中的图像
我想将imageview保存到特定文件夹中的图像,我尝试了这段代码,但它不起作用Android 将imageview保存到特定文件夹中的图像,android,file,imageview,Android,File,Imageview,我想将imageview保存到特定文件夹中的图像,我尝试了这段代码,但它不起作用 public void saveImage(View v){ View content = findViewById(R.id.iv_photo); content.setDrawingCacheEnabled(true); Bitmap bitmap = content.getDrawingCache(); //File file
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();
}
这应该可以解决问题。要从应用程序的资源中保存图像文件,您可以如下操作:
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();
}
这应该能奏效。我真的不明白你想要达到什么目的。您想将资源中的文件保存到sd卡或?@Android Developer是的,我有一个带有图像库的应用程序,我希望用户可以将其保存到自己的库中。我真的不明白您想要实现什么。你想从sd卡或@Android Developer中的资源中保存文件是的,我有一个带有图像库的应用程序,我希望用户可以将其保存到自己的库中。好的,这是从“R.drawable.my_image”保存图像,但如何从“R.id.imageView”保存图像为什么需要将imageView的id保存在sd卡中?因为我的应用程序中有从相机拍摄图像的选项,所以用户可以编辑颜色和许多其他选项,所有这些操作都是在imageview中进行的。您不应该保存imageview的id,而应该保存从相机返回并用于添加效果的位图/流。好的,这用于从“R.drawable.my_image”保存图像,但我如何从“R.id.imageview”保存图像为什么需要将imageView的id保存在sd卡中?因为我的应用程序中有从相机拍摄图像的选项,所以用户可以编辑颜色和许多其他选项,所有这些操作都是在imageview中进行的。您不应保存imageview的id,而应保存从相机返回的位图/流,并使用该位图/流添加效果。