0

I am developing an Android app.

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?

Dragon warrior
  • 1,644
  • 2
  • 24
  • 37

1 Answers1

2

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

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

ContentResolver cr = getContentResolver();
InputStream is = cr.openInputStream(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 file path from caught DownloadManager intent

Community
  • 1
  • 1
Flávio Faria
  • 6,575
  • 3
  • 39
  • 59
  • I can't get he file instance by calling **File f = new File(new URI(uri.toString()));**. I get a runtime exception indicated "java.lang.IllegalArgmentException: Expected file scheme in URI : content://...". – Dragon warrior Apr 17 '13 at 20:00
  • Please post here the value of `uri.toString()`. – Flávio Faria Apr 17 '13 at 20:27
  • Open with Email: content://com.android.email.attachmentprovider/1/121/RAW – Dragon warrior Apr 17 '13 at 20:38
  • Open with gmail: content://gmail-ls/my_gmail_account/message/7869/attachments/0.1/BEST/false – Dragon warrior Apr 17 '13 at 20:39
  • 1
    not able to open the attachment with my app using the above code. value of `uri.tostring()` is `content://com.google.android.email.attachmentprovider/4/941/RAW` and exception I am getting is `FileNotFound`. – Mayank May 21 '14 at 06:33