在保存照片后使用MediaScanner进行媒体文件扫描,以便将文件添加到媒体库中,最终在图库中显示。
代码示例:
private void saveImage(Bitmap bitmap) {
String fileName = //在此处设置文件名和保存路径
File file = new File(fileName);
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
//通知媒体库更新
MediaScannerConnection.scanFile(getContext(),
new String[]{file.getAbsolutePath()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
//在此处更新UI
}
});
}