向 MediaStore.Images.Media.insertImage 寫入後,必須發送廣播讓系統媒體掃描器更新資料庫,否則圖庫、QQ 等無法立即看到。
String path = Images.Media.insertImage(getContentResolver(), bitmap, title, description);
if (path != null) {
Uri uri = Uri.parse(path);
// 發送廣播掃描單一文件
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
}
或儲存檔案後:
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "pic.jpg");
// 存檔...
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
Android 10+ (Q) 建議使用 MediaStore API 直接插入,無需掃描廣播。