简体   繁体   English

如何使用FileProvider共享图像文件

[英]How can I use FileProvider to share a Image file

How can I use FileProvider to share a Image file. 如何使用FileProvider共享图像文件。 My app has a image file in the files/attach directory that I want to share the file. 我的应用程序在files / attach目录中有一个图像文件,我要共享该文件。 I am using a FileProvider to get Uri to share. 我正在使用FileProvider让Uri分享。 However, I get a " java.lang.IllegalArgumentException". 但是,我收到了“java.lang.IllegalArgumentException”。

private void shareNote() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_SUBJECT, shareNote.getTitle());
        intent.putExtra(Intent.EXTRA_TEXT, shareNote.getTitle());
        intent.setType("image/*");
        File attachFile = new File(shareFile.getFilePath()); 
        //FileProvider.getUriForFile() throws a "java.lang.IllegalArgumentException" Exception.
        Uri attachUri = FileProvider.getUriForFile(getContext(), "com.demoapp.rs.mynotebook.fileprovider", attachFile);
        intent.putExtra(Intent.EXTRA_STREAM, attachUri);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        getContext().startActivity(intent);
}

AndroidManifest.xml AndroidManifest.xml中

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demoapp.rs.mynotebook">
<application>
<uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application ...>
    <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.demoapp.rs.mynotebook.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true"
            >
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>
    </application>
</manifest>

filepaths.xml filepaths.xml

<paths>
    <files-path name="ifiles"  path="attach/"/>
    <external-path name="ifiles" path="attach/" />
</paths>

the share image file is at directory "/data/data/com.demoapp.rs.mynote/files/attach/F2000001.png" the Exception detials: 共享映像文件位于目录“/data/data/com.demoapp.rs.mynote/files/attach/F2000001.png”异常详细信息:

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.demoapp.rs.mynote/files/attach/F2000001.png at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:678) at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377) at com.demoapp.rs.component.ShareNoteDialog.shareNote(ShareNoteDialog.java:107) at com.demoapp.rs.component.ShareNoteDialog.access$000(ShareNoteDialog.java:29) at com.demoapp.rs.component.ShareNoteDialog$1.onItemClick(ShareNoteDialog.java:72) ... java.lang.IllegalArgumentException:无法在android.support.v4.content.FileProvider $ SimplePathStrategy.getUriForFile(FileProvider。)中找到包含/data/data/com.demoapp.rs.mynote/files/attach/F2000001.png的已配置根目录。 java:678)位于com.demoapp.rs.component的com.demoapp.rs.component.ShareNoteDialog.shareNote(ShareNoteDialog.java:107)的android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377) .ShareNoteDialog.access $ 000(ShareNoteDialog.java:29)at com.demoapp.rs.component.ShareNoteDialog $ 1.onItemClick(ShareNoteDialog.java:72)...

Then I modify the code like this: 然后我像这样修改代码:

 private void shareNote() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_SUBJECT, shareNote.getTitle());
        intent.putExtra(Intent.EXTRA_TEXT, shareNote.getTitle());
        intent.setType("image/*");

//        File attachFile = new File(shareFile.getFilePath());
//        Uri attachUri = FileProvider.getUriForFile(getContext(), "com.demoapp.rs.mynotebook.fileprovider", attachFile);
//        intent.putExtra(Intent.EXTRA_STREAM, attachUri);
        Uri attachFileUri = Uri.parse("content://com.demoapp.rs.mynotebook.fileprovider/ifiles/F2000001.png");
        intent.putExtra(Intent.EXTRA_STREAM, attachFileUri);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        getContext().startActivity(intent);
    }

Then I send a Email successfull, but the email does not has the shared image file as attach. 然后我成功发送电子邮件,但电子邮件没有附加的共享图像文件。

And I modify the code again. 然后我再次修改代码。 I use the original Java code, and modify the filepaths.xml file like this: 我使用原始Java代码,并修改filepaths.xml文件,如下所示:

<paths>
    <files-path name="ifiles"  path="attach/"/>
    <!--<external-path name="ifiles" path="attach/" />-->
</paths>

Then I can send email, and I can see the attach file and the file size at the send email userinterface! 然后我可以发送电子邮件,我可以在发送邮件用户界面上看到附件文件和文件大小!

But after I open the received email, has no attach file. 但在我打开收到的电子邮件后,没有附件。 Why? 为什么?

I use the FileProvider, AbstractFileProvider, and the configuration in AndroidManifest.xml from https://github.com/commonsguy/cw-omnibus/tree/master/ContentProvider/Files , and it works. 我使用了FileProvider,AbstractFileProvider以及来自https://github.com/commonsguy/cw-omnibus/tree/master/ContentProvider/Files的 AndroidManifest.xml中的配置,它可以正常工作。 But there's some email servers can't get the shared file still, when some email servers are OK. 但是当某些电子邮件服务器没问题时,有些电子邮件服务器无法获取共享文件。 I don't know why. 我不知道为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM