Recently, I encountered saving photos and videos in the project. They have been saved locally but cannot be found in the album and project. Here is a brief summary.
After saving locally, you need to send the file to the local or broadcast to refresh the album
1. Send the photo to the album
// Insert the file into the system gallery /** * @param context * @param targetFile photo file to save * @param path The path address of the photo to be saved */ public static void addMediaStore(Context context, File targetFile, String path) { ContentResolver resolver = context.getContentResolver(); ContentValues newValues = new ContentValues(5); newValues.put(MediaStore.Images.Media.DISPLAY_NAME, targetFile.getName()); newValues.put(MediaStore.Images.Media.DATA, targetFile.getPath()); newValues.put(MediaStore.Images.Media.DATE_MODIFIED, System.currentTimeMillis() / 1000); newValues.put(MediaStore.Images.Media.SIZE, targetFile.length()); newValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, newValues); MediaScannerConnection.scanFile(context, new String[]{path}, null , null ); // Refresh the album }
sample code
addMediaStore(chatActivity, mFile, mFile.getAbsolutePath());
(context, file, and file path)
2. Send the video to the album
// For non-system video resource folder public static void insertIntoMediaStore(Context context, boolean isVideo, File saveFile, long createTime) { ContentResolver mContentResolver = context.getContentResolver(); if (createTime == 0) createTime = System.currentTimeMillis(); ContentValues values = new ContentValues(); values.put(MediaStore.MediaColumns.TITLE, saveFile.getName()); values.put(MediaStore.MediaColumns.DISPLAY_NAME, saveFile.getName()); // The value is the same, but it is still treated with constants values.put(isVideo ? MediaStore.Video.VideoColumns.DATE_TAKEN : MediaStore.Images.ImageColumns.DATE_TAKEN, createTime); values.put(MediaStore.MediaColumns.DATE_MODIFIED, System.currentTimeMillis()); values.put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis()); if (!isVideo) values.put(MediaStore.Images.ImageColumns.ORIENTATION, 0); values.put(MediaStore.MediaColumns.DATA, saveFile.getAbsolutePath()); values.put(MediaStore.MediaColumns.SIZE, saveFile.length()); values.put(MediaStore.MediaColumns.MIME_TYPE, isVideo ? getVideoMimeType下面的方法/*"video/3gp"*/ : "image/jpeg"); //插入 mContentResolver.insert(isVideo ? MediaStore.Video.Media.EXTERNAL_CONTENT_URI : MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); }
// here is a sample code for a call (context, is it video, file, time)
insertIntoMediaStore(chatActivity,true,dest,0);
This can also be used as an album to refresh the gallery
. Here we need a related method to get the video format
// Get the mine_type of the video, temporarily only support 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"; }
3. Generic method (send broadcast to notify album refresh)
/** * Only need to scan for system folders, no need to insert content provider, otherwise it will be repeated * * @param context context * @param filePath 文件路径 */private 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); }
Invocation example (context, and path to file)
scanFile(chatActivity,/*FileUtil.getCacheFilePath(video_file_name)*/ dest.getAbsolutePath());
To sum up, here are all systematic methods. After a little modification according to the project, you can directly call and modify it according to your own needs.
leileitua