Flutter分享图片意图

4

我将尝试通过经典的Intent分享一张图片。我已经添加了以下内容:

file_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path name="my_images" path="." />
</paths>

清单:

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

最后是MainActivity.java:

private void shareFile(String fileName) {
        Intent share = new Intent(Intent.ACTION_SEND);
        Uri uri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", new File(this.getApplicationInfo().dataDir + "/app_flutter/userphotos", fileName));
        share.setData(uri);
        share.setType("image/png");
        share.putExtra(Intent.EXTRA_STREAM, uri);
        share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(Intent.createChooser(share, "Share"));
    }

我面临的问题是,我尝试共享的图像具有以下路径:
/data/user/0/shoedrobe.innovativeproposals.com.shoedrobe/app_flutter/userphotos/receipt20181101094430.jpg
然而,FileProvider 尝试从此处访问它:
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/shoedrobe.innovativeproposals.com.shoedrobe/app_flutter/userphotos/receipt20181101094430.jpg
为了保存图像,我使用 path_provider 包,并将项目保存在 getApplicationDocumentsDirectory() 下,在 Android 中为 AppData 目录。
我不确定为什么 FileProvider 突然决定从 /data/user/0/ 转到 /data/data/ 文件夹,因此任何有关此事的帮助或提示将不胜感激。
更新: 我已经按推荐更新了代码,并在 MainActivity.java 中用以下行替换了 Uri:
Uri uri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", new File(this.getDir("flutter", Context.MODE_PRIVATE).getPath() + "/app_flutter/userphotos", path));

然而,问题仍然存在(同样的异常,文件应该位于/data/data/<package>而不是/data/user/0)。我还尝试在我的AndroidManifest中添加额外的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

但是它没有很好地工作。问题可能出在我的 file_paths.xml 文件上吗?

2个回答

1
最终,所有提出的解决方案都没有奏效;无论是使用extenal-path还是其他解决方案。
最终我成功共享文件的方式是先将其复制到临时(缓存)目录中(在Flutter中使用path_providergetTemporaryDirectory()函数),然后将file_paths.xml更新为以下内容:
<cache-path name="image" path="."/>

最后,在 MainActivity.java 下面:
Uri uri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", new File(this.getCacheDir(), path));

0

在幕后, Flutter的getApplicationDocumentsDirectory()使用getDir()而不是dataDir,这可能会有所不同。

根据Android文档中关于getDataDir()的说明,它等同于this.getApplicationInfo().dataDir

返回文件系统上存储此应用程序所有私有文件的目录的绝对路径。 应用程序不应直接使用此路径;相反,它们应该使用此类上的getFilesDir()、getCacheDir()、getDir(String, int)或其他存储API。

如果调用应用程序被移动到采用存储设备,则返回的路径可能会随时间而变化,因此只应持久化相对路径。

为了确保Flutter保存图像的目录与通过FileProvider检索文件的目录之间的一致性,可以修改MainActivity.java中的getUriForFile行,如下所示:
FileProvider.getUriForFile(this, "com.example.android.fileprovider", new File(<your application context>.getDir("flutter", Context.MODE_PRIVATE).getPath() + "/app_flutter/userphotos", fileName));

...将<your application context>替换为存储您的应用程序上下文的变量(如果有)。


能否分享必要的代码更改来修复应用程序? - Robert J.
我已经修改了答案,并提供了一个可能的解决方案,其中涉及将用于获取图像的目录从您现有的dataDir更改为Flutter源代码中的Context.getDir() - Carol Ng
我已经根据建议更新了我的代码(请查看帖子更新),但是相同的问题仍然存在(使用不同的路径)。由于路径问题仍然存在,我应该使用不同的路径来存储项目吗?或者我应该更新我的file_paths文件吗? - Robert J.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接