转载自 转载原文链接
这篇文章不是完全的原创,因为有借鉴了网上的一些资料,一个辅助类AlbumNotifyHelper.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.provider.MediaStore; import android.util.Log; import java.io.File; /** * CreateAt : 2017/5/24 * Describe : 相册更新通知帮助类 * 创建时间单位ms * 视频时长单位ms * */ public class AlbumNotifyHelper { public static final String TAG = AlbumNotifyHelper.class.getSimpleName(); /// // 下面是对外公开的重载的方法 /// public static void notifyScanDcim(Context context, String filePath) { scanFile(context, filePath); } public static void insertVideoToMediaStore(Context context, String filePath, long dateTaken, long duration) { insertVideoToMediaStore(context, filePath, dateTaken, 0, 0, duration); } /* public static void insertVideoToMediaStore(Context context, VideoUtil.VideoInfo videoInfo) { insertVideoToMediaStore(context, videoInfo.originalVideoFilePath, videoInfo.dateTaken, videoInfo.width, videoInfo.height, videoInfo.duringTime); }*/ public static void insertImageToMediaStore(Context context, String filePath, long createTime) { insertImageToMediaStore(context, filePath, createTime, 0, 0); } /// // 扫描系统相册核心方法 /// /** * 针对系统文夹只需要扫描,不用插入内容提供者,不然会重复 * * @param context 上下文 * @param filePath 文件路径 */ public static void scanFile(Context context, String filePath) { if (!checkFile(filePath)) return; Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); intent.setData(Uri.fromFile(new File(filePath))); context.sendBroadcast(intent); } /// // 非系统相册像MediaContent中插入数据,核心方法 /// /** * 针对非系统文件夹下的文件,使用该方法 * 插入时初始化公共字段 * * @param filePath 文件 * @param time ms * @return ContentValues */ private static ContentValues initCommonContentValues(String filePath, long time) { ContentValues values = new ContentValues(); File saveFile = new File(filePath); long timeMillis = getTimeWrap(time); values.put(MediaStore.MediaColumns.TITLE, saveFile.getName()); values.put(MediaStore.MediaColumns.DISPLAY_NAME, saveFile.getName()); values.put(MediaStore.MediaColumns.DATE_MODIFIED, timeMillis); values.put(MediaStore.MediaColumns.DATE_ADDED, timeMillis); values.put(MediaStore.MediaColumns.DATA, saveFile.getAbsolutePath()); values.put(MediaStore.MediaColumns.SIZE, saveFile.length()); return values; } /** * 保存到照片到本地,并插入MediaStore以保证相册可以查看到,这是更优化的方法,防止读取的照片获取不到宽高 * * @param context 上下文 * @param filePath 文件路径 * @param createTime 创建时间 <=0时为当前时间 ms * @param width 宽度 * @param height 高度 */ public static void insertImageToMediaStore(Context context, String filePath, long createTime, int width, int height) { if (!checkFile(filePath)) return; createTime = getTimeWrap(createTime); ContentValues values = initCommonContentValues(filePath, createTime); values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, createTime); values.put(MediaStore.Images.ImageColumns.ORIENTATION, 0); values.put(MediaStore.Images.ImageColumns.ORIENTATION, 0); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) { if (width > 0) values.put(MediaStore.Images.ImageColumns.WIDTH, 0); if (height > 0) values.put(MediaStore.Images.ImageColumns.HEIGHT, 0); } values.put(MediaStore.MediaColumns.MIME_TYPE, getPhotoMimeType(filePath)); context.getApplicationContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); } /** * 保存到视频到本地,并插入MediaStore以保证相册可以查看到,这是更优化的方法,防止读取的视频获取不到宽高 * * @param context 上下文 * @param filePath 文件路径 * @param createTime 创建时间 <=0时为当前时间 ms * @param duration 视频长度 ms * @param width 宽度 * @param height 高度 */ public static void insertVideoToMediaStore(Context context, String filePath, long createTime, int width, int height, long duration) { if (!checkFile(filePath)) return; createTime = getTimeWrap(createTime); ContentValues values = initCommonContentValues(filePath, createTime); values.put(MediaStore.Video.VideoColumns.DATE_TAKEN, createTime); if (duration > 0) values.put(MediaStore.Video.VideoColumns.DURATION, duration); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) { if (width > 0) values.put(MediaStore.Video.VideoColumns.WIDTH, width); if (height > 0) values.put(MediaStore.Video.VideoColumns.HEIGHT, height); } values.put(MediaStore.MediaColumns.MIME_TYPE, getVideoMimeType(filePath)); context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values); } // 是不是系统相册 private static boolean isSystemDcim(String path) { return path.toLowerCase().contains("dcim") || path.toLowerCase().contains("camera"); } // 获取照片的mine_type private static String getPhotoMimeType(String path) { String lowerPath = path.toLowerCase(); if (lowerPath.endsWith("jpg") || lowerPath.endsWith("jpeg")) { return "image/jpeg"; } else if (lowerPath.endsWith("png")) { return "image/png"; } else if (lowerPath.endsWith("gif")) { return "image/gif"; } return "image/jpeg"; } // 获取video的mine_type,暂时只支持mp4,3gp private static String getVideoMimeType(String path) { String lowerPath = path.toLowerCase(); if (lowerPath.endsWith("mp4") || lowerPath.endsWith("mpeg4")) { return "video/mp4"; } else if (lowerPath.endsWith("3gp")) { return "video/3gp"; } return "video/mp4"; } // 获得转化后的时间 private static long getTimeWrap(long time) { if (time <= 0) { return System.currentTimeMillis(); } return time; } // 检测文件存在 private static boolean checkFile(String filePath) { //boolean result = FileUtil.fileIsExist(filePath); boolean result = false; File mFile = new File(filePath); if (mFile.exists()){ result = true; } Log.e(TAG, "文件不存在 path = " + filePath); return result; }
2.下载成功后调用,传入下载成功的图片或者视频的本地地址刷新手机图库
复制代码
1
2private ArrayList<String> mListPathSysImg = new ArrayList<>();//通知系统图库专用
以下是调用方法,把它替换成你的就可以
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17if(mListPathSysImg.size()!=0){ //下载成功,刷新系统图库 视频部分手机不支持显示(如ivo,魅蓝) for (int i=0;i<mListPathSysImg.size();i++){ String nDownLoadPath = mListPathSysImg.get(i); String updateImgPath = FileUtil.getDownLoad()+nDownLoadPath.substring(nDownLoadPath.lastIndexOf("/")); if(updateImgPath.contains(".LRV")){ updateImgPath = updateImgPath.replace(".LRV",".MP4"); AlbumNotifyHelper.insertVideoToMediaStore(this,updateImgPath,0,5000); }else{ AlbumNotifyHelper.insertImageToMediaStore(this,updateImgPath,0); } //Log.e("nDownLoadPath----more-->","nDownLoadPath="+nDownLoadPath+",updateImgPath="+updateImgPath); } mListPathSysImg.clear(); }
最后
以上就是烂漫火最近收集整理的关于android开发,APP的图片或者视频显示在手机自带的相册里转载自 转载原文链接的全部内容,更多相关android开发,APP的图片或者视频显示在手机自带的相册里转载自内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复