android - 如何从 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 不包含有关它无法打开的文件的详细信息。我也没有在系统日志中看到任何表明问题可能是什么的东西;我在这里显示的输出全部来自我代码中的打印语句。我错过了什么?
解决方案
推荐阅读
- swift - 当另一个模块在 Swift 中具有具有自己名称的类型时,引用另一个模块中与当前模块中的类型同名的类型
- python - python中什么是更好的迭代?
- dns - CentOS/RHEL 中的“_gateway”变量是什么东西?
- python - 使用终端打开时,由于导入错误,Jupyter Notebook 无法打开
- java - Java.util.zip.ZipException:在 ZipOutputStream 中使用 putNextEntry() 时条目大小无效(预期为 18401 但得到 0 个字节)?
- azure - 如何在 Service Fabric 部署期间保留有状态服务的状态?
- python - 使用带有各种列的 Pandas 数据框进行绘图
- python - PEP 8 - 通过添加空格来对齐参数
- quarkus - Quarkus - 覆盖 SecurityContext 不适用于 quarkus-resteasy-reactive,但适用于 quarkus-resteasy
- c - 如何在 C 中的函数中使用指针(指向矩阵)作为参数?