捕获的图像未存储在 android 11 中

Pri*_*Bnr 6 android android-11

我无法将捕获的图像存储在 (getExternalFilesDir(Environment.DIRECTORY_PICTURES)) Android 11 设备中。

<uses-permissionandroid:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>也添加了清单和所有文件访问权限。但这不起作用。

if (Build.VERSION.SDK_INT >= 30) {
            if (!Environment.isExternalStorageManager()) {
                try {
                    val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
                    intent.addCategory("android.intent.category.DEFAULT")
                    intent.data = Uri.parse(String.format("package:%s", applicationContext.packageName))
                    startActivityForResult(intent, 2296)
                } catch (e: Exception) {
                    val intent = Intent()
                    intent.action = Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
                    startActivityForResult(intent, 2296)
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

此代码可在 Android 11 设备下运行。但在 Android 11 上文件未创建File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) .toString() + "/" + FolderName )

Qaz*_*han 4

您的手机相机无权在指定位置写入。因此,要解决此问题,您需要使用文件提供程序并为其授予适当的权限,以便相机可以将图像写入您的文件。

要做到这一点,

  1. 创建一个文件提供者。在清单文件中添加:
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />        // <-------- see this
        </provider>
Run Code Online (Sandbox Code Playgroud)

files.xml现在在您的文件夹中创建一个文件res/xml。在其中编写一些代码:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <cache-path
        name="camera"
        path="Camera/" />
    <cache-path
        name="cache"
        path="/" />
    <files-path
        name="files"
        path="." />
    <external-path
        name="external"
        path="." />
    <external-files-path
        name="my_images"
        path="/"/>
// todo: add necessary folders according to your requirements...
// also, this is an old example. Consider googling for the latest style. I'm just copying from an old project I have, and it kinda works...
</paths>
Run Code Online (Sandbox Code Playgroud)

因此,这里我们为 FileProvider 提供可以与外部应用程序共享的文件夹。2. 现在创建一个要存储照片的 uri。在您的活动中:

Context applicationContext = getApplicationContext();
File root = getCachedDir(); // consider using getExternalFilesDir(Environment.DIRECTORY_PICTURES); you need to check the file_paths.xml
        File capturedPhoto = new File(root, "some_photo.jpeg");
        if(!photoFile.exists()) {
            photoFile.mkdirs();
        }
        Uri photoURI = FileProvider.getUriForFile(applicationContext, applicationContext.getPackageName() + ".fileprovider", capturedPhoto);
Run Code Online (Sandbox Code Playgroud)

请注意,我的项目需要临时保存图片,所以我使用了cachedDir。如果您永久保存照片,请getExternalFilesDir(Environment.DIRECTORY_PICTURES);正确使用和修改file_paths.xml。

  1. 现在我们有了正确的 uri,我们可以调用相机意图:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
        startActivityForResult(takePictureIntent, REQUEST_CODE);
Run Code Online (Sandbox Code Playgroud)
  1. 最后,在活动结果中,做一些事情:
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
  if(requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
    // todo: maybe show photo in an imageView
}
}
Run Code Online (Sandbox Code Playgroud)

我希望这能起作用。

编辑

如果您在生产中使用此应用程序,那么依赖 Android 的默认相机应用程序是一个坏主意。我们的应用程序以前使用过这种方式,并且它可以与三星的默认相机一起使用。但我们的许多用户使用第三方应用程序,例如PixArt,它不会将照片保存到我们给定的位置。因此我们必须使用CameraX. 因此,请考虑使用 CameraX 或其他一些相机库。