Android 圖片保存到相冊(cè)不顯示的解決方案(兼容Android 10及更高版本)
寫了一個(gè)demo,簡(jiǎn)單邏輯就是:在一個(gè)圖片上添加一行文字或者是水印,并且保存到系統(tǒng)相冊(cè),也就是我們手機(jī)上的圖庫(kù)。前面編輯圖片添加水印都沒有問(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ō)沒有更新到系統(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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 父div高度不能自適應(yīng)子div高度的解決方案2. ASP.NET MVC解決上傳圖片臟數(shù)據(jù)的方法3. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)4. JSP狀態(tài)管理的簡(jiǎn)單介紹5. jsp+mysql實(shí)現(xiàn)網(wǎng)頁(yè)的分頁(yè)查詢6. servlet+jsp實(shí)現(xiàn)過(guò)濾器 防止用戶未登錄訪問(wèn)7. 選擇模式 - XSL教程 - 28. Java之JSP教程九大內(nèi)置對(duì)象詳解(中篇)9. 淺談XML Schema中的elementFormDefault屬性10. ASP中SELECT下拉菜單同時(shí)獲取VALUE和TEXT值的實(shí)現(xiàn)代碼
