权限被拒绝:正在读取android.support.v4.content.FileProvider URI ANDROID馅饼

问题描述 投票:0回答:1

我无法共享屏幕图像,它在旧版本中运行良好,只是在android pie上不起作用,我收到此错误,有人可以帮助我吗?

错误

java.lang.SecurityException: Permission Denial: reading android.support.v4.content.FileProvider uri content://myapp.com.provider/external/screenshot.png from pid=29478, uid=1000 requires the provider be exported, or grantUriPermission()

代码

  fun shareIt(imagePath: File? = this.imagePath) {

        val uri = FileProvider.getUriForFile(context!!, BuildConfig.APPLICATION_ID+ ".provider", imagePath!!)

        val sharingIntent = Intent(Intent.ACTION_SEND)
        sharingIntent.type = "image/*"
        val shareBody = "APP"
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "app share")
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody)
        sharingIntent.putExtra(Intent.EXTRA_STREAM, uri)

        startActivity(Intent.createChooser(sharingIntent, "SHARE VIA"))
    }

清单代码

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

路径代码:

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

有人知道我应该怎么做才能解决这个问题?欢迎任何帮助。

android android-studio kotlin share android-9.0-pie
1个回答
0
投票

就像错误消息说的一样:

  • 您必须设置android:exported="true"(它将显示它)或
  • 您必须在其上使用方法grantUriPermission()(授予访问权限)。

建议从不赞成使用的android.support.v4.content.FileProvider迁移到android.support.v4.content.FileProvider

© www.soinside.com 2019 - 2024. All rights reserved.