¿Cómo puedo conseguir que el tipo mime de un archivo tenga su Uri?


Tengo una Lista de Uri obtenidos con la Galería y la Cámara. Estos Uris son así: content://media/external/images/media/94. ¿Cómo puedo obtener su tipo mime?

Author: Brais Gabin, 2012-09-18

4 answers

Puedes probar

ContentResolver cR = context.getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getExtensionFromMimeType(cR.getType(uri));

Editar:

mime.getExtensionFromMimeType(cR.getType(uri)) 

Devuelve - > "jpeg"

cR.getType(uri);

Devuelve "image/jpeg" que es el valor esperado.

 139
Author: Kartik Domadiya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-19 03:32:14

Este método devuelve la extensión del archivo (jpg, png, pdf, epub, etc..).

 public static String getMimeType(Context context, Uri uri) {
    String extension;

    //Check uri format to avoid null
    if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
        //If scheme is a content
        final MimeTypeMap mime = MimeTypeMap.getSingleton();
        extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
    } else {
        //If scheme is a File
        //This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file name with spaces and special characters.
        extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString());

    }

    return extension;
}
 23
Author: Aaron,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-09 09:09:46

En lugar de esto:

String type = mime.getExtensionFromMimeType(cR.getType(uri));

Haz esto:

String type = cR.getType(uri);

Y obtendrás esto: image/jpeg.

 7
Author: Mohamed Ibrahim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-21 16:38:14

Para Content Uri.

ContentResolver cr = context.getContentResolver();
mimeType = cr.getType(contentUri);

Para File Uri.

String fileExtension = MimeTypeMap.getFileExtensionFromUrl(fileUri
            .toString());
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
            fileExtension.toLowerCase());

Para ambos, funciona tanto para Content como para File.

public String getMimeType(Context context, Uri uri) {
    String mimeType = null;
    if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
        ContentResolver cr = context.getContentResolver();
        mimeType = cr.getType(uri);
    } else {
        String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
                .toString());
        mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
                fileExtension.toLowerCase());
    }
    return mimeType;
}
 4
Author: Bhavesh Rangani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-30 09:30:28