-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMediaUtil.java
More file actions
65 lines (61 loc) · 2.51 KB
/
MediaUtil.java
File metadata and controls
65 lines (61 loc) · 2.51 KB
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
package common.base.utils;
import android.content.Context;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;
/**
* ******************(^_^)***********************<br>
* User: fee(QQ/WeiXin:1176610771)<br>
* Date: 2019/1/8<br>
* Time: 22:39<br>
* <P>DESC:
* 系统媒体的工具类,后续增加通用获取媒体文件的方法
* </p>
* ******************(^_^)***********************
*/
public class MediaUtil {
/**
* 通知系统进行媒体文件的扫描任务
* @param context
*/
public static void notifySysMedia2Scan(Context context, MediaScannerConnection.OnScanCompletedListener callback) {
notifySysMedia2Scan(context, callback, null);
}
/**
* 通过广播的方式通知系统扫描某个文件
* @param appContext appContext
* @param filePath 要被扫描的文件路径
*/
public static void notifySysScanFile(Context appContext, String filePath) {
Uri data = Uri.parse("file://" + filePath);
Intent toScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data);
appContext.sendBroadcast(toScanIntent);
}
public static void notifySysMedia2Scan(Context context, MediaScannerConnection.OnScanCompletedListener callback, String toScanTheFilePath) {
if (null == toScanTheFilePath) {
toScanTheFilePath = Environment.getExternalStorageDirectory().getAbsolutePath();
}
if (!Util.isCompateApi(19)) {//android 4.4系统之前,发广播的方式通知系统去扫描
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + toScanTheFilePath)));
}
else{
MediaScannerConnection.scanFile(context, new String[] {toScanTheFilePath}, null, callback);
}
}
public static void notifyAddedImg(Context appContext,File newAddedImgFile) {
if (newAddedImgFile != null && newAddedImgFile.exists()) {
String fileAbsPath = newAddedImgFile.getAbsolutePath();
String fileNmae = newAddedImgFile.getName();
// try {//这个会再生成一个缩略图
// MediaStore.Images.Media.insertImage(appContext.getContentResolver(),fileAbsPath, newAddedImgFile.getName(), fileNmae);
// } catch (Exception e) {
// e.printStackTrace();
// }
notifySysScanFile(appContext,fileAbsPath);
}
}
}