【问题标题】:Sharing internal storage using FileProvider使用 FileProvider 共享内部存储
【发布时间】:2017-11-19 17:49:12
【问题描述】:

我想要的是在我的应用程序之间共享文件。我尝试使用文件提供程序,但它没有用。

首先在我的插件应用程序中,我设置了一个文件提供程序:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mdl.test.plug">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".ModuleActivity"
        android:label="@string/title_activity_module"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.mdl.test.plug.fileprovider"
        android:grantUriPermissions="true"
        android:exported="false">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths" />
    </provider>

</application>

我的文件路径.xml:

 <paths>
    <files-path path="/" name="allfiles" />
</paths

在一个核心应用程序中,我尝试阅读 test.txt:

Uri uri = Uri.parse("content://com.mdl.test.plug.fileprovider/allfiles/test.txt");
    InputStream is = null;
    StringBuilder result = new StringBuilder();
    try {
        is = getApplicationContext().getContentResolver().openInputStream(uri);
        BufferedReader r = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = r.readLine()) != null) {
            result.append(line);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try { if (is != null) is.close(); } catch (IOException e) { }
    }

但它返回一个权限错误:

Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord

我想要的是 Core 可以从 Plug 读取文件,而不是 Plug 将文件发送到 Core。有什么解决办法吗?

谢谢

【问题讨论】:

  • 您尚未告知您的 test.txt 文件所在的位置。请告知完整路径。此外,您应该使用 getUriForFile() 来设置 uri。

标签: android sharing android-fileprovider


【解决方案1】:

但它返回一个权限错误

这是因为核心应用程序没有该Uri 的权限。插件应用程序必须授予您权限,让插件应用程序使用一些基于Intent 的IPC(startActivity()startService()sendBroadcast() 等),其中您包括Uri Intent 的“数据”方面并使用 FLAG_GRANT_READ_URI_PERMISSION

我想要的是 Core 可以从 Plug 读取文件,而不是 Plug 将文件发送到 Core

如果不允许任何其他应用程序从插件应用程序读取文件,这很难做到。

如果您的 minSdkVersion 为 21 或更高,您可以创建自己的 ContentProvider 导出并使用自定义权限保护自己。核心应用程序将具有用于该权限的&lt;uses-permission&gt; 元素,然后它可以随时请求该ContentProvider。如果您授予自定义权限 android:protectionLevelsignature,那么您的通信应该是安全的,尽管它需要核心应用程序和插件应用程序由相同的签名密钥签名。但是,如果您的minSdkVersion 低于 21,则自定义权限存在一些安全问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多