美文网首页
Mimetype和媒体库更新操作

Mimetype和媒体库更新操作

作者: 2eb56199844d | 来源:发表于2017-07-07 15:20 被阅读21次

// url = file path or whatever suitable URL you want.
public static String getMimeType(String url)
{
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extension);
}
return type;
}

Android通过广播更新文件和文件夹到媒体库
媒体库更新

  1. 解决办法:
    1 . 将Intent换为 ACTION_MEDIA_SCANNER_SCAN_FILE
    .
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    但是这个Intent只扫描文件,不扫描文件夹。如果你的应用只操作单个文件而不是文件夹,就可以使用这个。 2. 不发广播,换为MediaScannerConnection进行通知更新
    MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null, new MediaScannerConnection.OnScanCompletedListener() { public void onScanCompleted(String path, Uri uri) { Log.i("ExternalStorage", "Scanned " + path + ":"); Log.i("ExternalStorage", "-> uri=" + uri); } });

相关文章

网友评论

      本文标题:Mimetype和媒体库更新操作

      本文链接:https://www.haomeiwen.com/subject/qezlhxtx.html