【问题标题】:Android app Mp3 file mimetype resolving to nullAndroid 应用 Mp3 文件 mimetype 解析为 null
【发布时间】:2015-11-30 04:00:19
【问题描述】:

我正在尝试获取存储在下载中的 mp3 的 mimetype。问题是 mimetype 总是返回 null。我不确定为什么。请注意,将扩展名设为小写 (.mp3) 并没有什么不同。

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

【问题讨论】:

    标签: java android mp3 mime-types android-contentresolver


    【解决方案1】:

    检查以下代码。你会知道为什么你没有得到所需的输出。

        String type = null;
        String extension = "mp3";
    
        type = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    
        Log.d(TAG,"mime type1 "+type);
    
        extension = "MP3";
    
        type = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    
        Log.d(TAG,"mime type2 "+type);
    

    如果您无法通过将文件扩展名设为小写来做出任何改变,请考虑使用

    File filesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File mov = new File(filesDir,"song.mp3");
    

    并使用文件的绝对路径而不是硬编码路径来构造 uri。

    【讨论】:

      猜你喜欢
      • 2020-08-05
      • 1970-01-01
      • 2013-12-09
      • 1970-01-01
      • 2016-08-28
      • 2011-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多