如何将布局或视图作为图像导出到 Android 中的可见画廊文件夹?
How can I export Layout or Views as images to visible gallery folder in Android?
搜索 "How to save Layout views as images" 后,我找到了一些可以保存在内部和外部存储中的解决方案。但似乎创建的图像文件将保存在一些通常不可见的 data/data/... 文件夹中。实际上,我希望用户在画廊中看到图像。我找到了如下代码,但我什至无法检查图像是否已创建:
View content = findViewById(R.id.relativeLayout);
String yourimagename = "MyImageFile";
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/" + yourimagename + ".png");
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 10, ostream);
ostream.close();
content.invalidate();
} catch (Exception e) {
e.printStackTrace();
} finally {
content.setDrawingCacheEnabled(false);
}
But It seems the image file created is going to save in some data/data/... folder that is not visible normally.
文件将保存在程序员选择的保存位置。
Actually I want the image visible in gallery for the user. I've found some code like below, but I even can't check if the image is created or not
该代码不适用于 Android 的任何版本,因为 new File("/" + yourimagename + ".png")
不会为您提供可用的 File
,因为它指向一个您既不能不读也不写。
欢迎您将图片保存到internal storage or external storage。由于您希望 "gallery" 类型的应用程序获取此图像,因此最好选择外部存储,然后使用 MediaScannerConnection
及其 scanFile()
方法来获取由 MediaStore
,因为图库应用倾向于使用 MediaStore
作为它们的图像来源。
总的来说,我担心getDrawingCache()
会不靠谱。你最好告诉你的根 View
取而代之的是你自己的 Bitmap
支持的 Canvas
。
搜索 "How to save Layout views as images" 后,我找到了一些可以保存在内部和外部存储中的解决方案。但似乎创建的图像文件将保存在一些通常不可见的 data/data/... 文件夹中。实际上,我希望用户在画廊中看到图像。我找到了如下代码,但我什至无法检查图像是否已创建:
View content = findViewById(R.id.relativeLayout);
String yourimagename = "MyImageFile";
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/" + yourimagename + ".png");
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 10, ostream);
ostream.close();
content.invalidate();
} catch (Exception e) {
e.printStackTrace();
} finally {
content.setDrawingCacheEnabled(false);
}
But It seems the image file created is going to save in some data/data/... folder that is not visible normally.
文件将保存在程序员选择的保存位置。
Actually I want the image visible in gallery for the user. I've found some code like below, but I even can't check if the image is created or not
该代码不适用于 Android 的任何版本,因为 new File("/" + yourimagename + ".png")
不会为您提供可用的 File
,因为它指向一个您既不能不读也不写。
欢迎您将图片保存到internal storage or external storage。由于您希望 "gallery" 类型的应用程序获取此图像,因此最好选择外部存储,然后使用 MediaScannerConnection
及其 scanFile()
方法来获取由 MediaStore
,因为图库应用倾向于使用 MediaStore
作为它们的图像来源。
总的来说,我担心getDrawingCache()
会不靠谱。你最好告诉你的根 View
取而代之的是你自己的 Bitmap
支持的 Canvas
。