首页 > 解决方案 > Open internal storage image with Intent.ACTION_EDIT exception with FileProvider android

问题描述

In my application there is a need where i have to EDIT an internal storage image and save the new to internal storage again. So i have tried to make an Intent.ACTION_EDIT in order to use the default Gallery app for editing the image.

if (it.fileName.fileType() == IMAGE_TYPE) {
   createEditImageFile()
   val intent = Intent(Intent.ACTION_EDIT).apply {
      flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
      putExtra(MediaStore.EXTRA_OUTPUT,
      FileProvider.getUriForFile(this@MyActivity,BuildConfig.APPLICATION_ID,
       File(currentEditPhotoPath)))
      setDataAndType(uri, "image/*")
   }
   startActivity(intent)
   return
   }

So i get hte following exception which is reasonable since i dont have my FileProvider exported in the Manifest.

java.lang.SecurityException: Permission Denial: writing androidx.core.content.FileProvider uri content://{package_name}/external_path_files/Pictures/JPEG_20200720_163239_edit_5491322030710880687.jpg from pid=26681, uid=10206 requires the provider be exported, or grantUriPermission()

So when i change my in the Manifest.xml file to

<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}"
            android:exported="true"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_provider_paths" />
        </provider>
        <provider
            android:name="androidx.work.impl.WorkManagerInitializer"
            android:authorities="${applicationId}.workmanager-init"
            android:exported="false"
            tools:node="remove" />

and the paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path
        name="files"
        path="." />

    <external-files-path
        name="external_path_files"
        path="." />

    <external-path
        name="external_files"
        path="."/>
</paths>

CreateEditImageFile() if the following

@Throws(IOException::class)
    private fun createEditImageFile(): File {
        val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
        val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        return File.createTempFile("JPEG_${timeStamp}_edit_", ".jpg", storageDir).apply {
            currentEditPhotoPath = absolutePath
        }
    }

i have the following fail on the Build.

Manifest merger failed : Attribute provider#androidx.core.content.FileProvider@exported value=(false) from AndroidManifest.xml:158:13-37
    is also present at AndroidManifest.xml:167:13-36 value=(true).
    Suggestion: add 'tools:replace="android:exported"' to <provider> element at AndroidManifest.xml:155:9-163:20 to override.

I dont really understand the error, and the most weird is that the AndroidManifest.xml lines are not where the provider is defined. How can i solve this? Thank you

标签: androidkotlinandroid-intentandroid-fileprovider

解决方案


推荐阅读