【发布时间】: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