Android开发之刷新图片到相册 | 刷新视频到相册的方法区分发广播刷新方法
生活随笔
收集整理的這篇文章主要介紹了
Android开发之刷新图片到相册 | 刷新视频到相册的方法区分发广播刷新方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我們很多app會有保存圖片和保存視頻,保存成功后一般在最近文件或者相冊就能看到了,這個需要我們在保存文件后自行刷新到相冊中,以前老版本方法通過廣播刷新方法在API29中已經廢棄了無法使用,咱們提供了新版本方法如下圖:
//說明第一個參數上下文,第二個參數是文件路徑例如:/storage/emulated/0/1621832516463_1181875151.mp4 第三個參數是文件類型,傳空代表自行根據文件后綴判斷刷新到相冊 MediaScannerConnection.scanFile(context, new String[]{fileNamePath}, null, new MediaScannerConnection.OnScanCompletedListener() {@Overridepublic void onScanCompleted(String path, Uri uri) {//刷新成功的回調方法Log.e("資源刷新成功路徑為", path)}});看下Google官方說明文檔
Google官方文檔說明需要科學上網:MediaScannerConnection.scanFile方法說明
Google官方文檔說明無需科學上網:MediaScannerConnection.scanFile方法說明
看下完整代碼:
package cn.xiayiye5.xiayiye5library.utils;import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Intent; import android.media.MediaScannerConnection; import android.net.Uri; import android.provider.MediaStore; import android.util.Log;import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL;/*** @author : xiayiye5* @date : 2021/5/21 17:45* 類描述 :*/ public class DownLoadUtils {public static final String IMG_URL = "https://ae01.alicdn.com/kf/Ua227945b506241af975a9b0a16d6df3bA.jpg";public static final String VIDEO_URL = "https://www.w3school.com.cn/example/html5/mov_bbb.mp4";private DownLoadUtils() {}public static DownLoadUtils getInstance() {return SingleObject.DOWN_LOAD_UTILS;}private static class SingleObject {private static final DownLoadUtils DOWN_LOAD_UTILS = new DownLoadUtils();}public void download(Activity activity, String url, String fileNamePath, String mediaType) {try {URL url1 = new URL(url);HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();urlConnection.setRequestMethod("GET");urlConnection.setConnectTimeout(30_000);urlConnection.setReadTimeout(30_000);int responseCode = urlConnection.getResponseCode();if (responseCode == 200) {InputStream inputStream = urlConnection.getInputStream();FileOutputStream fileOutputStream = new FileOutputStream(fileNamePath);readFile5(inputStream, fileOutputStream);refreshApi29(activity, fileNamePath); // refreshMinApi29(activity, fileNamePath, mediaType);}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static ContentValues getVideoContentValues(File paramFile, long paramLong) {ContentValues localContentValues = new ContentValues();localContentValues.put(MediaStore.Video.Media.TITLE, paramFile.getName());localContentValues.put(MediaStore.Video.Media.DISPLAY_NAME, paramFile.getName());localContentValues.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");localContentValues.put(MediaStore.Video.Media.DATE_TAKEN, Long.valueOf(paramLong));localContentValues.put(MediaStore.Video.Media.DATE_MODIFIED, Long.valueOf(paramLong));localContentValues.put(MediaStore.Video.Media.DATE_ADDED, Long.valueOf(paramLong));localContentValues.put(MediaStore.Video.Media.DATA, paramFile.getAbsolutePath());localContentValues.put(MediaStore.Video.Media.SIZE, Long.valueOf(paramFile.length()));return localContentValues;}/*** 讀寫方式五* 高效字節流方式一個字節一個字節的讀寫*/private void readFile5(InputStream fileInputStream, FileOutputStream fileOutputStream) {try {BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);//代表讀取到的數據的底層int值int ch = 0;while ((ch = bufferedInputStream.read()) != -1) {Log.e("下載資源", ch + "");bufferedOutputStream.write(ch);}bufferedOutputStream.flush();bufferedInputStream.close();bufferedOutputStream.close();} catch (IOException e) {e.printStackTrace();}}/*** 刷新圖片和視頻到相冊低版本方法** @param activity 當前頁面* @param fileNamePath 文件路徑* @param mediaType 刷新的文件類型*/private void refreshMinApi29(Activity activity, String fileNamePath, String mediaType) {if (mediaType.contains("image")) {File file = new File(fileNamePath); // try { // //將圖片插入到相冊 // MediaStore.Images.Media.insertImage(activity.getContentResolver(), file.getAbsolutePath(), file.getName(), null); // } catch (FileNotFoundException e) { // Log.i("Exception Msg", Log.getStackTraceString(e)); // } // activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + fileNamePath)));//將圖片掃描到相冊中顯示ContentResolver localContentResolver = activity.getContentResolver();ContentValues localContentValues = getImageContentValues(file, System.currentTimeMillis());localContentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, localContentValues);Intent localIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");final Uri localUri = Uri.fromFile(file);localIntent.setData(localUri);activity.sendBroadcast(localIntent);} else {//刷新視頻到相冊方法二 API29以下的老方法,在API29中已棄用!ContentResolver localContentResolver = activity.getContentResolver();ContentValues localContentValues = getVideoContentValues(new File(fileNamePath), System.currentTimeMillis());Uri localUri = localContentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, localContentValues);activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri));}}public static ContentValues getImageContentValues(File paramFile, long paramLong) {ContentValues localContentValues = new ContentValues();localContentValues.put("title", paramFile.getName());localContentValues.put("_display_name", paramFile.getName());localContentValues.put("mime_type", "image/jpeg");localContentValues.put("datetaken", Long.valueOf(paramLong));localContentValues.put("date_modified", Long.valueOf(paramLong));localContentValues.put("date_added", Long.valueOf(paramLong));localContentValues.put("orientation", Integer.valueOf(0));localContentValues.put("_data", paramFile.getAbsolutePath());localContentValues.put("_size", Long.valueOf(paramFile.length()));return localContentValues;}/*** 刷新視頻到相冊方法一 API通用方法包括API29及以上高版本方法** @param activity 當前頁面* @param fileNamePath 文件路徑*/private void refreshApi29(Activity activity, String fileNamePath) {//刷新相冊,mineTypes為null的話讓系統自己根據文件后綴判斷文件類型MediaScannerConnection.scanFile(activity, new String[]{fileNamePath}, null, (path, uri) -> Log.e("資源刷新成功路徑為", path));//代表只刷新視頻格式為mp4類型其它格式視頻文件不刷新 // MediaScannerConnection.scanFile(activity, new String[]{fileNamePath}, new String[]{"video/mp4"}, (path, uri) -> Log.e("資源刷新成功路徑為", path));//代表刷新視頻文件,只要是視頻都刷新根據當前Android系統支持哪些視頻格式進行刷新 // MediaScannerConnection.scanFile(activity, new String[]{fileNamePath}, new String[]{"video/*"}, (path, uri) -> Log.e("資源刷新成功路徑為", path));//代表只刷新圖片格式為jpg的文件到相冊中 // MediaScannerConnection.scanFile(activity, new String[]{fileNamePath}, new String[]{"image/jpg"}, (path, uri) -> Log.e("資源刷新成功路徑為", path));//代表刷新圖片到相冊只要是圖片就會刷新 // MediaScannerConnection.scanFile(activity, new String[]{fileNamePath}, new String[]{"image/*"}, (path, uri) -> Log.e("資源刷新成功路徑為", path));} }調用上面工具類:
package cn.xiayiye5.xiayiye5library.activity;import android.os.Environment; import android.view.View;import java.util.Random;import cn.xiayiye5.xiayiye5library.R; import cn.xiayiye5.xiayiye5library.utils.DownLoadUtils; import cn.xiayiye5.xiayiye5library.utils.ThreadUtils;/*** @author : xiayiye5* @date : 2021/5/21 17:36* 類描述 : 下載圖片視頻刷新到相冊的方法*/ public class SaveVideoAndImgActivity extends BaseActivity {@Overrideprotected int getLayoutView() {return R.layout.activity_video_and_img;}@Overrideprotected void initId() {}@Overrideprotected void loadData() {}public void savePicture(View view) {//下載圖片之前記得開啟讀取sd卡權限String fileNamePath = Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + "_" + new Random().nextInt(Integer.MAX_VALUE) + ".jpg";ThreadUtils.getInstance().createThread(() -> DownLoadUtils.getInstance().download(SaveVideoAndImgActivity.this, DownLoadUtils.IMG_URL, fileNamePath, "image/jpg")); // new Thread(() -> DownLoadUtils.getInstance().download(this, DownLoadUtils.IMG_URL, fileNamePath)).start();}public void saveVideo(View view) {//下載視頻之前記得開啟讀取sd卡權限String fileNamePath = Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + "_" + new Random().nextInt(Integer.MAX_VALUE) + ".mp4";ThreadUtils.getInstance().createThread(() -> DownLoadUtils.getInstance().download(SaveVideoAndImgActivity.this, DownLoadUtils.VIDEO_URL, fileNamePath, "video/mp4")); // new Thread(() -> DownLoadUtils.getInstance().download(this, DownLoadUtils.VIDEO_URL, fileNamePath)).start();} }我是將文件報訊到SD卡根目錄,然后在相冊進行顯示的看圖:
感謝博主提供老的刷新圖片和視頻到相冊的方法:博主老API刷新圖片視頻到相冊的方法
上面有兩種刷新圖片和視頻的方法:分為API29以下和API29以上
個人建議使用新方法:MediaScannerConnection.scanFile方法說明
Google官方文檔將圖片刷新到相冊的方法:將圖片添加到圖庫官方文檔
可查看完整項目:保存圖片和視頻到相冊的方法
總結
以上是生活随笔為你收集整理的Android开发之刷新图片到相册 | 刷新视频到相册的方法区分发广播刷新方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Minisforum 新款 HX80G
- 下一篇: 《永劫无间》x《卧龙:苍天陨落》双向联动