Java 使用getExternalFilesDir(字符串)、MediaStore或Intent#ACTION _OPEN_DOCUMENT将文件存储在应用程序文件夹的外部路径中?
我正在开发一个应用程序,我需要的一个功能是,我可以打开相机拍摄一张照片,并将其存储在路径图片/Example中。我将图像保存在该路径中,以便可以从google photos应用程序打开它 使用此片段,我创建图像的路径和名称,并打开相机:Java 使用getExternalFilesDir(字符串)、MediaStore或Intent#ACTION _OPEN_DOCUMENT将文件存储在应用程序文件夹的外部路径中?,java,android,android-studio,Java,Android,Android Studio,我正在开发一个应用程序,我需要的一个功能是,我可以打开相机拍摄一张照片,并将其存储在路径图片/Example中。我将图像保存在该路径中,以便可以从google photos应用程序打开它 使用此片段,我创建图像的路径和名称,并打开相机: private void openCameraTocaptureImage() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Ensure
private void openCameraTocaptureImage() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.cameraimage.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
调用createImageFile()
到目前为止,该应用程序满足了我的需要,拍摄了一张照片并将其存储在图片/示例中,并且可以从照片应用程序中查看,但阅读我发现:
getExternalStorageDirectory
此方法在API级别29中被弃用。
为了提高用户隐私,不推荐直接访问共享/外部存储设备。当应用程序以Build.VERSION_CODES.Q为目标时,此方法返回的路径将不再可供应用程序直接访问。应用程序可以通过迁移到上下文#getExternalFilesDir(字符串)、MediaStore或Intent#ACTION#OPEN#文档等备选方案,继续访问存储在共享/外部存储上的内容
我决定使用getExternalFilesDir
,所以我尝试替换
File storageDir=新文件(Environment.getExternalStorageDirectory(),Environment.DIRECTORY_PICTURES+“/Example”)代码>
与
File storageDir=getExternalFilesDir(Environment.DIRECTORY\u PICTURES+“/Example”)代码>
代码如下所示:
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES+"/Example");
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
但这会导致我的照片存储在Android/data/com.example.cameraimage/files/Pictures/example中,这不是我想要的路径,也不会显示在照片应用程序中
主要问题是,我是否应该停止使用getExternalStorageDirectory()
,即使它可以工作?
如果我必须更改如何使用任何备选方案,以便我可以将照片存储在图片/示例中,并在照片应用程序中显示图像?“我无法将照片存储在我的Android模拟器10中的应用程序文件夹“不匹配”之外,并且Android R对我来说是完全可用的getExternalStorageDirectory()”。您可能希望编辑您的问题,并提供您正在尝试的内容和结果的详细信息。@Commonware道歉可能我无法解释,我已经编辑了问题,抱歉英语不好“一个问题是,为什么如果getExternalStorageDirectory被弃用,它在android 10和android R上仍然适用于我?”--您的清单中的
元素上可能有android:requestLegacyExternalStorage=“true”
。一旦你的targetSdkVersion
达到29,Android 10应该停止工作,并且应该在今年晚些时候这样做,以便能够在Play Store(和其他一些分销渠道)上发布你的应用程序。谢谢,我对这一点不熟悉,但我已经了解了一些事情,最后一个问题,您知道如何使用Context#getExternalFilesDir(String)、MediaStore或Intent#ACTION(打开)文档将照片存储在应用程序本身的文件夹之外吗?对于ACTION(打开)文档
,您应该能够传递进入额外输出的Uri
。对于MediaStore
,理论上,您应该能够insert()
一个条目,并传递通过额外输出收到的Uri
getExternalFilesDir()
特别指出,不会在应用程序唯一的外部存储位置之外进行写入。
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES+"/Example");
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}