1

I made an image viewer that (supposedly) opens attachments from email clients, that already opens correctly PNG files. In particular, I use K9, that sends an ACTION_VIEW intent with (basically) this code:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityIfAvailable(getContext(), intent);

When I click the attachment, my image viewer reports the following contentURI URL:

content://com.fsck.k9.tempfileprovider/temp/00ac996dc9f9539488ba4a6a349a8bf567267f5d?mime_type=image%2Fpng

I cannot find the /temp folder, so I guess I'm doing something wrong, and/or I don't know how to handle "providers"? How do I open this URI?

Luis A. Florit
  • 2,169
  • 1
  • 33
  • 58

1 Answers1

0

How do I open this URI?

The same way you open any Uri with a content or file scheme:

  • Call getContentResolver() on a Context to get a ContentResolver
  • Call openInputStream() on the ContentResolver to get an InputStream on the content identified by the Uri
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Sorry, I don't understand, I should have been more explicit. My method only needs to retrieve the filename where k9 stored the attachment (the decoding, etc will be done elsewhere). In the above example, the `context.getContentResolver()` only has two columns, the URI fileName `00ac996dc9f9539488ba4a6a349a8bf567267f5d`, and the file size. It has no columnName `_data`. I called the `getContentResolver()` with: `Cursor cursor = context.getContentResolver().query(contentURI, null, null, null, null);` – Luis A. Florit Jan 01 '18 at 19:59
  • @LuisA.Florit: "My method only needs to retrieve the filename where k9 stored the attachment" -- if the `Uri` has a `content` scheme, [it does not have to point to a file, let alone a file that you can access](https://stackoverflow.com/a/35870955/115145). – CommonsWare Jan 01 '18 at 20:03
  • The inputStream is a `ParcelFileDescriptor`, in the original case, it reads `android.os.ParcelFileDescriptor$AutoCloseInputStream@425c23a0` – Luis A. Florit Jan 01 '18 at 20:49
  • @LuisA.Florit: "The inputStream is a ParcelFileDescriptor" -- no, it is not. It is a `ParcelFileDescriptor.AutoCloseInputStream`. Here is [the JavaDocs for this class](https://developer.android.com/reference/android/os/ParcelFileDescriptor.AutoCloseInputStream.html). From your standpoint, you should stick with the methods defined on `InputStream`, as other versions of Android return other `InputStream` implementations. – CommonsWare Jan 01 '18 at 21:45
  • Sorry, if I understood correctly, I should create a new file from the FileDescriptor since there is no easy way to retrieve the original filename? My app opens images based on filenames only... – Luis A. Florit Jan 02 '18 at 01:33
  • @LuisA.Florit: Then set up your `` to only accept `file` as a scheme, if you can only deal with files. Or remove the limitation that you need a filename. Or use `DocumentFile` to get the "display name" and work with that, as occasionally that will actually be a traditional filename. – CommonsWare Jan 02 '18 at 01:57
  • Ok, I will think more about this and decide what is best. Thanks! – Luis A. Florit Jan 02 '18 at 12:00