What can we put as parameter of okhttp3 MediaType.parse method for an audio file?

ThiTrou picture ThiTrou · Mar 22, 2016 · Viewed 9.5k times · Source

I found that for uploading images using Android with a RequestBody, we can use MediaType.parse("image/jpeg").

However, what would be the parameter for an audio file ?

Thank you in advance for your answer.

Answer

Rohit Arya picture Rohit Arya · Mar 22, 2016

You can get mime type of any file using this method:

public static String getMimeType(String url) {
     String type = null;
     String extension = MimeTypeMap.getFileExtensionFromUrl(url);
     if (extension != null) {
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
     }
     return type;
}

To get mime, call String mime = getMimeType(file.getAbsolutePath());

You can find list of all mimes here. And thus, mime for audio will be audio/mpeg. But I will still recommend using getMimeTypeFromExtension than hardcoding the value.