【问题标题】:Sharing a file in the raw folder with a FileProvider与 FileProvider 共享原始文件夹中的文件
【发布时间】:2016-08-03 11:52:25
【问题描述】:

我正在尝试传递驻留在我的应用程序的 res/raw 目录中的图像以及共享意图。

我遵循FileProvider docs 中描述的过程,这是我的代码:

AndroidManifest.xml

<application ...>
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.myapp.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">

        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/paths" />
    </provider>
</application>

res/xml/paths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="shared" path="./"/>
</paths>

我活动中的代码:

String shareToPackage = ...

File imageFile = new File(context.getFilesDir().getPath() + "/image");
if (!imageFile.exists()) { // image isn't in the files dir, copy from the res/raw
    final InputStream inputStream = context.getResources().openRawResource(R.raw.my_image);
    final FileOutputStream outputStream = context.openFileOutput("image", Context.MODE_PRIVATE);

    byte buf[] = new byte[1024];
    int len;
    while ((len = inputStream.read(buf)) > 0) {
        outputStream.write(buf, 0, len);
    }

    outputStream.close();
    inputStream.close();

    imageFile = new File(context.getFilesDir().getPath() + "/image");
}

if (!imageFile.exists()) {
    throw new IOException("couldn't find file");
}

final Uri uri = Uri.fromFile(imageFile);
context.grantUriPermission(shareToPackage, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_TEXT, "here's the image");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setPackage(shareToPackage);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);

由于无法访问我在其他应用中获取的文件,因此上述内容不起作用:

java.io.FileNotFoundException:FILE_PATH:打开失败:EACCES (权限被拒绝)

知道我在这里做错了什么吗?
谢谢。

【问题讨论】:

  • 你有没有解决过这个问题 - 面对完全相同的......
  • @brandall 不。我放弃了它,因为浪费了太多时间。如果您确实找到了解决方案,请在此处回答,以便将来其他人也不需要浪费太多时间。祝你好运!
  • 难以置信... 6 个小时的厕所已经在这上面了,因为事情应该如此简单。如果我解决它,我会回帖.....

标签: java android android-fileprovider


【解决方案1】:

去掉&lt;files-path&gt; 中的path 属性,因为这里不需要它,因为您从getFilesDir() 提供所有内容。

在创建File 对象时不要使用字符串连接。替换:

new File(context.getFilesDir().getPath() + "/image.png");

与:

new File(context.getFilesDir().getPath(), "image.png");

最重要的是,不要使用Uri.fromFile()Use FileProvider.getUriForFile()。就目前而言,您正在完成所有这些工作来设置FileProvider,然后您不会使用FileProvider 将内容提供给其他应用程序。

或者,摆脱所有这些,以及use my StreamProvider,它可以直接提供原始资源。

或者,编写您自己的ContentProvider,直接为原始资源提供服务。

【讨论】:

  • 谢谢。我更改了您建议的所有内容,获得意图的应用程序仍然无法访问该文件,但现在它说该文件不存在而不是说权限被拒绝。该文件确实存在,因为我在发送意图之前检查了该文件,所以这里发生了什么?你的StreamProvider 听起来很有趣,但我想先修复这个当前版本,然后我会试试你的。
  • @NitzanTomer:在outputStream.close(); 之前,添加outputStream.flush();outputStream.getFD().sync();,以确保所有字节都写入磁盘。另外,注释掉EXTRA_TEXT,因为ACTION_SEND 支持EXTRA_TEXTEXTRA_STREAM,不一定同时支持。您还可以记录您从FileProvider 获得的Uri,看看它是否有意义。
  • 添加了flushsync,删除了EXTRA_TEXT,但文件仍然不存在。 uri 对我来说似乎没问题:content://com.myapp.fileprovider/shared/image,但我不是专家,所以我可能错了。
  • @NitzanTomer:我同意Uri 看起来是正确的。这可能是其他应用程序中的错误。您可以尝试混合my LegacyCompatCursorWrapper,这有助于一些客户端应用程序。 This sample app 演示了它与 FileProvider 的使用。
  • 不,另一个应用程序是我自己的(因为我正在测试这个uri附件),我没有查询或任何东西,只是尝试根据收到的uri打开文件。我还尝试了您的StreamProvider,但仍然在接收应用程序上该文件不存在。我一定做错了什么,但不知道是什么
【解决方案2】:

@nitzan-tomer,见https://stackoverflow.com/a/33031091/966789

什么是运行时权限?

在 Android 6.0 Marshmallow 中,Google 引入了一种新的权限模型,让用户可以更好地了解应用程序为何会请求特定权限。与用户在安装时盲目地接受所有权限不同,现在会提示用户在应用程序使用过程中根据需要接受权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 2022-06-30
    相关资源
    最近更新 更多