久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

Android 圖片保存到相冊(cè)不顯示的解決方案(兼容Android 10及更高版本)

瀏覽:4日期:2022-09-19 10:31:56
前言

寫了一個(gè)demo,簡單邏輯就是:在一個(gè)圖片上添加一行文字或者是水印,并且保存到系統(tǒng)相冊(cè),也就是我們手機(jī)上的圖庫。前面編輯圖片添加水印都沒有問題,到后面保存到系統(tǒng)相冊(cè)出現(xiàn)了問題:顯示不出來圖片。

問題

在 Android 10 之前保存系統(tǒng)相冊(cè)的三步驟:

保存圖片到手機(jī) 把圖片插入到手機(jī)圖庫 發(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)圖庫 try {MediaStore.Images.Media.insertImage(context.getContentResolver(),file.getAbsolutePath(), fileName, null); } catch (FileNotFoundException e) {e.printStackTrace(); } // 最后通知圖庫更新 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse('file://' + path)));}

出現(xiàn)的問題:圖片不顯示,也就是說沒有更新到系統(tǒng)圖庫中。

細(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)));解決問題

下面是解決上面的問題,并兼容 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();}//再更新圖庫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版本以及更高版本使用上面的方法,才能有效解決不顯示圖片的問題。

做個(gè)記錄!

以上就是Android 圖片保存到系統(tǒng)相冊(cè)不顯示的解決方案(兼容Android 10及更高版本)的詳細(xì)內(nèi)容,更多關(guān)于Android 圖片保存到相冊(cè)不顯示的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 自拍视频网站 | 日本精品视频在线观看 | 一级欧美日韩 | 日本一本在线 | 欧美一区二区在线看 | 五月天婷婷在线视频 | 亚洲视频一区 | 国产在线精品一区二区三区 | 香蕉久久一区二区不卡无毒影院 | 欧美一区二区三区视频 | 亚洲国产在 | 黑人av| 毛片免费观看 | 成人在线视频播放 | 一区二区三区在线视频播放 | 久久久资源 | 一区二区三区在线观看视频 | 久久国内免费视频 | 国产福利一区二区 | 成人黄色片在线观看 | 亚洲午夜精品一区二区三区他趣 | 欧美日韩精品一区二区 | 在线一级视频 | 日韩在线观看一区二区 | 亚洲精品视频大全 | 欧美大片在线看免费观看 | 欧美激情亚洲 | 日韩第一区 | 国产欧美日韩综合精品 | 色综合激情 | 中文字幕在线第一页 | 国产乱码精品1区2区3区 | 黄色欧美一级片 | 日日做夜夜爽毛片麻豆 | 久久精品六 | 91在线区| 日本激情在线 | 欧美一区二区三区免费 | 欧美一级c片 | 亚洲精品一区二区三区蜜桃下载 | 在线观看亚洲一区二区三区 |