Android 圖片保存到相冊(cè)不顯示的解決方案(兼容Android 10及更高版本)
前言
寫(xiě)了一個(gè)demo,簡(jiǎn)單邏輯就是:在一個(gè)圖片上添加一行文字或者是水印,并且保存到系統(tǒng)相冊(cè),也就是我們手機(jī)上的圖庫(kù)。前面編輯圖片添加水印都沒(méi)有問(wèn)題,到后面保存到系統(tǒng)相冊(cè)出現(xiàn)了問(wèn)題:顯示不出來(lái)圖片。
問(wèn)題
在 Android 10 之前保存系統(tǒng)相冊(cè)的三步驟:
- 保存圖片到手機(jī)
- 把圖片插入到手機(jī)圖庫(kù)
- 發(fā)廣播更新
代碼如下:
public static void savePhotoAlbum(Context context, Bitmap bmp) { // 首先保存圖片 File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee"); if (!appDir.exists()) { appDir.mkdir(); } String fileName = System.currentTimeMillis() + ".jpg"; File file = new File(appDir, fileName); try { FileOutputStream fos = new FileOutputStream(file); bmp.compress(CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 其次把文件插入到系統(tǒng)圖庫(kù) try { MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null); } catch (FileNotFoundException e) { e.printStackTrace(); } // 最后通知圖庫(kù)更新 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path))); }
出現(xiàn)的問(wèn)題:圖片不顯示,也就是說(shuō)沒(méi)有更新到系統(tǒng)圖庫(kù)中。
細(xì)心的小伙伴會(huì)發(fā)現(xiàn),上段代碼有兩處地方廢棄的方法:
MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
解決問(wèn)題
下面是解決上面的問(wèn)題,并兼容 Android10 版本:
/** * 添加水印并保存到系統(tǒng)相冊(cè) */ private void imgMerge() { new Thread(() -> { try { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test); File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "newFile.jpg"); if (!file.exists()) { file.createNewFile(); } //添加水印文字位置。 Bitmap newBitmap = addTextWatermark(bitmap, "測(cè)試demo示例"); //保存到系統(tǒng)相冊(cè) savePhotoAlbum(newBitmap, file); } catch (Exception e) { e.printStackTrace(); } }).start(); } /** * 保存到相冊(cè) * * @param src 源圖片 * @param file 要保存到的文件 */ private void savePhotoAlbum(Bitmap src, File file) { if (isEmptyBitmap(src)) { return; } //先保存到文件 OutputStream outputStream; try { outputStream = new BufferedOutputStream(new FileOutputStream(file)); src.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); if (!src.isRecycled()) { src.recycle(); } } catch (FileNotFoundException e) { e.printStackTrace(); } //再更新圖庫(kù) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { ContentValues values = new ContentValues(); values.put(MediaStore.MediaColumns.DISPLAY_NAME, file.getName()); values.put(MediaStore.MediaColumns.MIME_TYPE, getMimeType(file)); values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM); ContentResolver contentResolver = getContentResolver(); Uri uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); if (uri == null) { return; } try { outputStream = contentResolver.openOutputStream(uri); FileInputStream fileInputStream = new FileInputStream(file); FileUtils.copy(fileInputStream, outputStream); fileInputStream.close(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } else { MediaScannerConnection.scanFile( getApplicationContext(), new String[]{file.getAbsolutePath()}, new String[]{"image/jpeg"}, (path, uri) -> { // Scan Completed }); } }
發(fā)送廣播和插入MediaProvider兩種方式添加圖片到相冊(cè),這兩種方式已經(jīng)官方廢棄了。在 Android 10版本以及更高版本使用上面的方法,才能有效解決不顯示圖片的問(wèn)題。
做個(gè)記錄!
以上就是Android 圖片保存到系統(tǒng)相冊(cè)不顯示的解決方案(兼容Android 10及更高版本)的詳細(xì)內(nèi)容,更多關(guān)于Android 圖片保存到相冊(cè)不顯示的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Android 版本、權(quán)限適配相關(guān)總結(jié)
- Android Studio 2020新版本卡在Gradle downloading/sync failed/下載緩慢/下載超時(shí)的問(wèn)題
- 詳解如何在Android studio中更新sdk版本和build-tools版本
- Android Studio下載、安裝和配置+SDK+tools下載(無(wú)敵超級(jí)詳細(xì)版本)
- Android Studio3.6.3 當(dāng)前最新版本數(shù)據(jù)庫(kù)查找與導(dǎo)出方法(圖文詳解)
- 詳解Android studio 3+版本apk安裝失敗問(wèn)題
- android studio 打包自動(dòng)生成版本號(hào)與日期,apk輸入路徑詳解
- Android開(kāi)發(fā)獲取手機(jī)Mac地址適配所有Android版本
- Android實(shí)現(xiàn)懸浮窗全系統(tǒng)版本
- 詳解Android版本適配:9.0 Pie
- Android ProductFlavor的使用詳解
相關(guān)文章
Android使用TextView,設(shè)置onClick屬性無(wú)效的解決方法
下面小編就為大家?guī)?lái)一篇Android使用TextView,設(shè)置onClick屬性無(wú)效的解決方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12MPAndroidChart 自定義圖表繪制使用實(shí)例
這篇文章主要為大家介紹了MPAndroidChart 自定義圖表繪制使用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android串口開(kāi)發(fā)之使用JNI實(shí)現(xiàn)ANDROID和串口通信詳解
這篇文章主要給大家介紹了關(guān)于Android串口開(kāi)發(fā)之使用JNI實(shí)現(xiàn)ANDROID和串口通信的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01利用百度地圖Android sdk高仿微信發(fā)送位置功能及遇到的問(wèn)題
這篇文章給大家介紹了利用百度地圖Android sdk高仿微信發(fā)送位置功能,在實(shí)現(xiàn)此功能的時(shí)候遇到點(diǎn)小問(wèn)題,下面小編給大家列出來(lái),需要的朋友參考下吧2017-12-12Android CalendarView,DatePicker,TimePicker,以及NumberPicker的使
這篇文章主要介紹了Android CalendarView,DatePicker,TimePicker,以及NumberPicker的使用的相關(guān)資料,需要的朋友可以參考下2016-12-12