我正在尝试使用android camera在一个名为“appFolder”的文件夹中保存图像。我的目标sdk是25.我的设备在android nougat上运行。但是,当我使用“dispatchTakePictureIntent()”单击图像时。图像不会保存在appFolder中。它会保存在DCIM /相机文件夹中。为什么会发生这种情况以及如何将其保存在我的自定义文件夹中?
private void dispatchTakePictureIntent() {
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
Log.i("imageCaptutreError", ex.getMessage());
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.abc.def",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "appFolder");
if (!folder.exists()) {
folder.mkdir();
}
File tempFile = new File(folder, "temp_image.png");
/*new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "appFolder" + File.separator + "temp_image.png");*/
mCurrentPhotoPath = tempFile.getAbsolutePath();
return tempFile;
}
Mainifest中的提供商
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.abc.def"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
@ xml / file_paths
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="appFolder/" />
</paths>
答案 0 :(得分:2)
部分原因是你没有在addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
上致电Intent
。目前,其他应用对Uri
确定的位置没有写入权限。
但是,请记住第三方相机应用程序存在错误。理想情况下,他们尊重EXTRA_OUTPUT
。但是,有些人不会:
EXTRA_OUTPUT
或content
Uri
中的EXTRA_OUTPUT
计划(即使Google自己的相机应用程序在2016年中期之前遇到此问题)< / LI>
FWIW,this sample app显示ACTION_IMAGE_CAPTURE
与FileProvider
一起使用。