簡體   English   中英

無法使用文件提供程序 java.lang.SecurityException 共享圖像:權限被拒絕:讀取 androidx.core.content.FileProvider uri

[英]Unable to share Image using FIle Provider java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri

java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://com.jjdj.jdjjd.jdjjdj/sdcard1/storage/emulated/0/Pictures/Paintings/Painting_440.png from pid=27901, uid=1000 requires the provider be exported, or grantUriPermission() at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:740) at android.content.ContentProvider.semEnforceReadPermission(ContentProvider.java:659) at android.content.ContentProvider$ Transport.enforceReadPermission(ContentProvider.java:602) at android.content.ContentProvider$Transport.enforceFilePermission(ContentProvider.java:593) at android.content.ContentProvider$Transport.openTypedAssetFile(ContentProvider.Z 93F725A07423FE1C889F448B33D21F46Z:507) at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:307) at android.os.Binder.execTransactInternal(Binder.java:1021) at android.os.Binder.execTransact(Binder.java:994)

下面的代碼已用於此:

private fun share(fileName: String) {
    val shareFile = File(getStoragePath(), fileName)
    val contentUri = FileProvider.getUriForFile(activity, SHARED_PROVIDER, shareFile)
    val intentBuilder = contentUri?.let {
        ShareCompat.IntentBuilder.from(activity)
            .setType("image/*")
            .addStream(it)
    }
    val chooserIntent = intentBuilder?.createChooserIntent()
    activity.startActivity(chooserIntent)
}

提供者在清單中傳遞:

  <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.jsjsj"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_provider_paths" />
        </provider>

路徑 Xml:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path
        name="Paintings"
        path="Paintings/" />
    <external-path
        name="Paintings"
        path="." />
    <external-files-path
        name="Paintings"
        path="." />
    <!-- FOR SD CARD-->
    <root-path
        name="sdcard1"
        path="." />
</paths>

我有一個類似的問題。 這就是我最終做的,它的設置與你的有點不同,但希望它有幫助。

                val myBitmap = //create a bitmap here

                val file = File(activity.externalCacheDir, "myFileName.png")
                val fileOutputStream = FileOutputStream(file)
                myBitmap.compress(Bitmap.CompressFormat.PNG, 80, fileOutputStream)
                fileOutputStream.flush()
                fileOutputStream.close()

                val fileProviderUri = FileProvider.getUriForFile(context, context.applicationContext.packageName
                        + ".provider", file);
                
                val sendIntent: Intent = Intent().apply {
                    action = Intent.ACTION_SEND
                    flags = Intent.FLAG_ACTIVITY_NEW_TASK
                    flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
                    putExtra(Intent.EXTRA_STREAM, fileProviderUri)
                    type = "image/png"
                }

             
                val shareIntent = Intent.createChooser(sendIntent, null)

                if (shareIntent.resolveActivity(context.packageManager) != null) {
                    context.startActivity(shareIntent)
                } else {
                    Toast.makeText(context, "Error Occurred Please Try Again", Toast.LENGTH_SHORT).show()
                }

清單提供者

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM