Code Snippet for Media on Android

A few days ago,I have wrote down this post http://androidyue.github.io/blog/2014/01/19/scan-media-files-in-android/. Now I will paste my code snippet.

MediaUtils.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

package com.mx.browser.utils; import java.io.File; import java.util.Locale; import com.mx.utils.FileUtils; import com.mx.utils.Log; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.media.MediaScannerConnection; import android.media.MediaScannerConnection.OnScanCompletedListener; import android.net.Uri; import android.provider.MediaStore.Audio; import android.provider.MediaStore.Images; import android.provider.MediaStore.Video; import android.text.TextUtils; /** * Utility Methods for Media Library Operations * @author androidyue * Referrer http://androidyue.github.io/blog/2014/01/19/scan-media-files-in-android/ */ public class MediaUtils { private static final String LOGTAG = "MediaUtils"; /** * Scan a media file by sending a broadcast.This is the easiest way. * 对方成功接收广播并处理条件 文件必须存在,文件路径必须以Environment.getExternalStorageDirectory().getPath() 的返回值开头 */ public static void sendScanFileBroadcast(Context context, String filePath) { File file = new File(filePath); Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)); context.sendBroadcast(intent); } /** * * @param context * @param paths File paths to scan * @param mimeTypes mimeTypes in array;it could be null;then * @param callback */ public static void scanFiles(Context context, String[] paths, String[] mimeTypes, OnScanCompletedListener callback) { if (null != paths && paths.length != 0) { MediaScannerConnection.scanFile(context, paths, mimeTypes, callback); } else { Log.i(LOGTAG, "scanFiles paths = null or paths.length=0 paths=" + paths); } } public static void scanFiles(Context context, String[] paths, String[] mimeTypes) { scanFiles(context, paths, mimeTypes, null); } public static void scanFiles(Context context, String[] paths) { scanFiles(context, paths, null); } public static int removeImageFromLib(Context context, String filePath) { ContentResolver resolver = context.getContentResolver(); return resolver.delete(Images.Media.EXTERNAL_CONTENT_URI, Images.Media.DATA + "=?", new String[]{filePath}); } public static int removeAudioFromLib(Context context, String filePath) { return context.getContentResolver().delete(Audio.Media.EXTERNAL_CONTENT_URI, Audio.Media.DATA + "=?", new String[] {filePath}); } public static int removeVideoFromLib(Context context, String filePath) { return context.getContentResolver().delete(Video.Media.EXTERNAL_CONTENT_URI, Video.Media.DATA + "=?", new String[] {filePath}); } public static int removeMediaFromLib(Context context, String filePath) { String mimeType = FileUtils.getFileMimeType(filePath); int affectedRows = 0; if (null != mimeType) { mimeType = mimeType.toLowerCase(Locale.US); if (isImage(mimeType)) { affectedRows = removeImageFromLib(context, filePath); } else if (isAudio(mimeType)) { affectedRows = removeAudioFromLib(context ,filePath); } else if (isVideo(mimeType)) { affectedRows = removeVideoFromLib(context, filePath); } } return affectedRows; } public static boolean isAudio(String mimeType) { return mimeType.startsWith("audio"); } public static boolean isImage(String mimeType) { return mimeType.startsWith("image"); } public static boolean isVideo(String mimeType) { return mimeType.startsWith("video"); } public static boolean isMediaFile(String filePath) { String mimeType = FileUtils.getFileMimeType(filePath); return isMediaType(mimeType); } public static boolean isMediaType(String mimeType) { boolean isMedia = false; if (!TextUtils.isEmpty(mimeType)) { mimeType = mimeType.toLowerCase(Locale.US); isMedia = isImage(mimeType) || isAudio(mimeType) || isVideo(mimeType); } return isMedia; } /** * Before using it,please do have a media type check. * @param context * @param srcPath * @param destPath * @return */ public static int renameMediaFile(Context context, String srcPath, String destPath) { removeMediaFromLib(context, srcPath); sendScanFileBroadcast(context, destPath); return 0; } }

FileUtils.java

1 2 3 4 5 6 7 8 9 10

public static String getFileMimeType(String filename) { if (TextUtils.isEmpty(filename)) { return null; } int lastDotIndex = filename.lastIndexOf('.'); String mimetype = MimeTypeMap.getSingleton().getMimeTypeFromExtension( filename.substring(lastDotIndex + 1).toLowerCase()); Log.i(LOGTAG, "getFileMimeType mimeType = " + mimetype); return mimetype; }

Others

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏小鹏的专栏

ubuntu下C++如何调用python程序,gdb调试C++代码

Linux下gdb调试C++代码:http://jingyan.baidu.com/article/acf728fd464984f8e410a369.html ...

35590
来自专栏增长技术

App Guide相关

##TourGuide https://github.com/worker8/TourGuide

10520
来自专栏码匠的流水账

聊聊spring cloud的AsyncLoadBalancerAutoConfiguration

本文主要研究一下AsyncLoadBalancerAutoConfiguration

13010
来自专栏Hongten

My Notepad

I have spent near more two weeks to write this Notepad application. At this mome...

13020
来自专栏Android干货

Android项目实战(八):列表右侧边栏拼音展示效果

31850
来自专栏SAP最佳业务实践

SAP S/4 HANA新变化-CO物料帐(for Ver.1610)

Material Ledger Actual Costing has been activated already in the system before t...

91850
来自专栏lgp20151222

RestTemplate的异常:Not enough variables available to expand

原因:RestTemplate使用出错,我的情况是不知道这里要求用RestTemplate的使用格式,应该很多人都是这样吧?不过,看了下RestTemplate...

17540
来自专栏闵开慧

运行wordcount时显示Could not obtain block

该文章接上面hadoop运行wordcount时卡住不动,接着下面 hadoop@ubuntu118:~/hadoop-1.0.2$ bi...

364140
来自专栏一个会写诗的程序员的博客

java.base.jmod

/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/jmods$ jmod list java....

18520
来自专栏码匠的流水账

聊聊reactive streams publisher的doOn方法

本文主要研究一下reactive streams Publisher的doOn方法

12110

扫码关注云+社区

领取腾讯云代金券