【问题标题】:How can I use FileProvider to share a Image file如何使用 FileProvider 共享图像文件
【发布时间】:2015-02-17 06:57:15
【问题描述】:

如何使用 FileProvider 共享图像文件。 我的应用程序在我要共享文件的 files/attach 目录中有一个图像文件。我正在使用 FileProvider 来让 Uri 共享。但是,我得到一个“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

<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>

文件路径.xml

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

共享图像文件位于目录“/data/data/com.demoapp.rs.mynote/files/attach/F2000001.png” 异常详情:

java.lang.IllegalArgumentException:找不到配置的根目录 包含 /data/data/com.demoapp.rs.mynote/files/attach/F2000001.png 在 android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:678) 在 android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377) 在 com.demoapp.rs.component.ShareNoteDialog.shareNote(ShareNoteDialog.java:107) 在 com.demoapp.rs.component.ShareNoteDialog.access$000(ShareNoteDialog.java:29) 在 com.demoapp.rs.component.ShareNoteDialog$1.onItemClick(ShareNoteDialog.java:72) ...

【问题讨论】:

    标签: android


    【解决方案1】:

    然后我这样修改代码:

     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);
        }
    

    然后我成功发送了一封电子邮件,但电子邮件没有作为附件共享的图像文件。

    【讨论】:

      【解决方案2】:

      然后我再次修改代码。 我使用原始 Java 代码,并像这样修改 filepaths.xml 文件:

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

      然后我可以发送邮件了,我可以在发送邮件的用户界面看到附件和文件大小!

      但是打开收到的邮件后,没有附件。为什么?

      【讨论】:

        【解决方案3】:

        我使用 https://github.com/commonsguy/cw-omnibus/tree/master/ContentProvider/Files 中的 FileProvider、AbstractFileProvider 和 AndroidManifest.xml 中的配置,它可以工作。但是,当一些电子邮件服务器正常时,有些电子邮件服务器仍然无法获取共享文件。我不知道为什么。

        【讨论】:

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