首页 > 解决方案 > 如何从 VIEW Intent 中读取电子邮件附件

问题描述

我正在尝试在我的 Android 应用程序中阅读电子邮件附件。Intent 过滤器正在工作:当我在 GMail 应用程序中点击附件时,我的应用程序被列为打开附件的候选对象之一,当我选择它时,会调用相应的 Activity 的 onCreate() 方法。但是,当我尝试从 Intent 中读取数据时,我得到了 FileNotFoundException。

我的附件处理活动中的 onCreate() 方法如下所示(为简洁而编辑):

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String action = intent.getAction();
    System.err.println("FileImportActivity action = " + action);
    Uri uri = intent.getData();
    System.err.println("FileImportActivity uri = " + uri);
    InputStream is = null;
    try {
        is = getContentResolver().openInputStream(uri);
        // Read the attachment from the InputStream and process it
    } catch (IOException e) {
        // [...]
    }
    finish();
}

我得到这个输出:

FileImportActivity action = android.intent.action.VIEW
FileImportActivity uri = content://com.android.email.attachmentprovider/1/17/RAW
java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
        at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:144)
        at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:698)
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1458)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1295)
        at android.content.ContentResolver.openInputStream(ContentResolver.java:1015)
        at com.thomasokken.free42.FileImportActivity.onCreate(FileImportActivity.java:40)

Intent 和 Uri 看起来不错,getContentResolver().openInputStream(uri) 调用是我见过的示例都说打开附件的方式。FileNotFoundException 不包含有关它无法打开的文件的详细信息。我也没有在系统日志中看到任何表明问题可能是什么的东西;我在这里显示的输出全部来自我代码中的打印语句。我错过了什么?

标签: androidandroid-intentemail-attachments

解决方案


推荐阅读