【问题标题】:How can make a FileProvider available to other applications?如何使 FileProvider 可用于其他应用程序?
【发布时间】:2014-06-06 21:10:24
【问题描述】:

是否可以让其他应用程序使用 FileProvider ?

manifest.xml

...
 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.myapp.fileprovider"
        android:exported="true"
        android:grantUriPermissions="false"
        android:permission="com.example.filesvisible.permission.READ" >
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filespath" />
    </provider>
...

来自doc

false:提供程序不可用于其他应用程序。放 android:exported="false" 将提供程序的访问权限限制为您的 应用程序。仅具有相同用户 ID (UID) 的应用程序 提供者将有权访问它。

我尝试将导出设置为 true,但出现此异常 Unable to get provider android.support.v4.content.FileProvider: java.lang.SecurityException: Provider must not be exported 为什么我不能导出 FileProvider ?

【问题讨论】:

    标签: android provider android-contentresolver android-fileprovider


    【解决方案1】:

    是否可以让其他应用程序使用 FileProvider ?

    是的。这通常是重点。

    为什么我不能导出 FileProvider ?

    因为这不是您使用 FileProvider 的方式。

    FileProvider 后面的点是give select access to files to third-party apps。您可以通过FLAG_GRANT_READ_URI_PERMISSION 和/或FLAG_GRANT_WRITE_URI_PERMISSION 执行此操作,在Intent 中用于将您的提供商的Uri 值之一传递给第三方应用程序(例如,通过ACTION_VIEW IntentstartActivity()一起使用)。

    另见the training guide on sharing files

    【讨论】:

    • 嗨。我使用了您的建议,一切都很好,但是当我想使用蓝牙共享声音文件时,会发生错误并说蓝牙已停止。我认为这是因为蓝牙无权从我的应用程序文件夹中读取文件。我该如何解决这个问题?谢谢。
    • 这是我的代码:这是我的代码:sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(_context,"ir.khazan.ringtone.fileprovider",newSoundFile_in_app_folder)); sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); sendIntent.setType("音频/*"); Utils.activity.startActivity(sendIntent);
    • @karimkhan:“我该如何解决这个问题?” -- 使用 LogCat 检查与其他应用程序关联的 Java 堆栈跟踪。如果您不理解,请使用您的代码和堆栈跟踪询问有关 Stack Overflow 的单独问题。
    猜你喜欢
    • 2022-01-11
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 2016-05-31
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    相关资源
    最近更新 更多