2
我試圖獲取存儲在下載中的mp3的mimetype。問題是,mimetype總是返回null。我不確定爲什麼。請注意,使擴展名小寫(.mp3)沒有區別。Android應用程序Mp3文件mimetype解析爲零
String filePath = "/storage/emulated/0/Download/song.MP3";
File file = new File(filePath);
Uri uri2 = Uri.fromFile(file);
//will display /storage/emulated/0/Download/song.MP3
Log.d("file type as string",file.toString());
//will display file:///storage/emulated/0/Download/song.MP3
Log.d("uri type as string",uri2.toString());
ContentResolver cR = getApplicationContext().getContentResolver();
String mime = cR.getType(uri2);
//mime is null always displays
if(mime == null){
Log.d("result", "mime is null");
} else{
Log.d("mime", mime);
}
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(filePath);
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
System.out.println("extension:" +extension); //displays MP3
System.out.println("mimetype:" + mimetype); //displays null