我如何可以加载邮件附件我的应用程序?我的、应用程序、加载、邮件附件

2023-09-12 22:28:07 作者:傾〆壹世柔情ゝ

我开发一个Android应用程序。

I am developing an Android app.

我已经在的Manifest.xml添加一些设置如下。

I already add some setting in Manifest.xml as follow.

<intent-filter>
     <action android:name="android.intent.action.VIEW" />
     <category android:name="android.intent.category.DEFAULT" />
     <data android:mimeType="application/pdf" />
     <data android:mimeType="text/html" />
     <data android:mimeType="text/htm" />
</intent-filter>

然后,用户可以选择我的应用程序打开附件。

Then, the user can choose my app to open the attachment.

现在的问题是可以在那之后怎么办? 我如何可以访问该文件?

The question is what can I do after that? How can I access the file?

推荐答案

您会收到您声明此过滤器的活动的意图。然后,让你打开文件的URI:

You'll receive an intent in the activity you are declaring this filter. Then, get the URI of the file you're opening:

Intent intent = getIntent();
Uri uri = intent.getData();

这个URI可能会指向一个PDF文件。你可以阅读该文件是这样的:

This URI will probably point to a PDF file. You can read this file like this:

ContentResolver cr = getContentResolver();
InputStream is = cr.openInputStream(uri);

如果您在直接访问文件正在interessed,我建议你写你自己的文件输入流,因为这URI不一定解析到一个真实的文件,如果是的话,你可能没有权限阅读它。下面是关于这种情况下,提供了进一步的解释了一个问题:

If you're interessed in accessing the file directly, I suggest you write your own file from the input stream, since this uri may not necessarily resolve to a real file, and if it does, you may not have permission to read it. Here's a question regarding this case that provides further explanation:

Retrieve从抓下载管理意图文件路径