Android 以私密模式将照片保存到磁盘
Android save photo on disk in private mode
我可以使用以下方法将图像保存在磁盘中:
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = imageId + ".png";
String filePath = baseDir + File.separator + fileName;
File file = new File(filePath);
if(!file.exists())
{
out = new FileOutputStream(baseDir + File.separator + fileName);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
}
但是所有保存的图像都可以在 phone 的图库中看到。我怎样才能对用户隐藏它们?我的应用程序有私人文件夹吗?
谢谢
public File getAlbumStorageDir(Context context, String albumName) {
// Get the directory for the app's private pictures directory.
File file = new File(context.getExternalFilesDir(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
我想这就是你想要的。阅读 https://developer.android.com/training/basics/data-storage/files.html#WriteExternalStorage 了解更多信息。
使用Context.getFilesDir()
而不是Environment.getExternalStorageDirectory()
将其保存到内部存储器。
来自文档:
Files saved here are accessible by only your app by default.
检查 this link。
我可以使用以下方法将图像保存在磁盘中:
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = imageId + ".png";
String filePath = baseDir + File.separator + fileName;
File file = new File(filePath);
if(!file.exists())
{
out = new FileOutputStream(baseDir + File.separator + fileName);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
}
但是所有保存的图像都可以在 phone 的图库中看到。我怎样才能对用户隐藏它们?我的应用程序有私人文件夹吗?
谢谢
public File getAlbumStorageDir(Context context, String albumName) {
// Get the directory for the app's private pictures directory.
File file = new File(context.getExternalFilesDir(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
我想这就是你想要的。阅读 https://developer.android.com/training/basics/data-storage/files.html#WriteExternalStorage 了解更多信息。
使用Context.getFilesDir()
而不是Environment.getExternalStorageDirectory()
将其保存到内部存储器。
来自文档:
Files saved here are accessible by only your app by default.
检查 this link。