Question

In order to send an image to a server from my android application, I have to get mime type of the image (or, at least, its extension).

I get the image from a ACTION_GET_CONTENT intent. Some applications, like Dropbox, send a file:// scheme data, so I can guess the extension using MimeTypeMap.getFileExtensionFromUrl(), but some others, like Google Drive, send a content:// scheme, which not permit to do so.

I've tried many things before posting here, like this:

AssetFileDescriptor asset = getContentResolver().openAssetFileDescriptor(getIntent().getData(), "r");
FileInputStream stream = asset.createInputStream();
String mime = URLConnection.guessContentTypeFromStream(stream); // returns null

The only workaround I think is to decode the asset into a Bitmap, then generate a new compressed image (using Bitmap.compress()), but it will add some work to the phone, and it could change the format/quality of original image, so I reserve it only if there is no other solution.

Does anyone have an idea about my problem? Thanks a lot for help ;)

Was it helpful?

Solution

As suggested by @sh3psheep on Twitter, I've tried this, and it works for content:// schemes:

Uri data = getIntent().getData();
String mime = getContentResolver().getType(data); // returns correct MIME type, such as "image/png"

The only con I found is that it does not support file:// scheme, but we can handle it using method I described in question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top