public static void saveImageToGallery(Context context, Bitmap bmp) {
if (bmp == null){
ToastUtils.show(context, "保存出错了...");
return;
}
// 首先保存图片
File appDir = new File(BaseApplication.app.getTmpDir(), "ywq");
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = System.currentTimeMillis() + ".jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
ToastUtils.show(context, "文件未发现");
e.printStackTrace();
} catch (IOException e) {
ToastUtils.show(context, "保存出错了...");
e.printStackTrace();
}catch (Exception e){
ToastUtils.show(context, "保存出错了...");
e.printStackTrace();
}
// 最后通知图库更新
try {
MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
context.sendBroadcast(intent);
ToastUtils.show(context, "保存成功了...");
}
将图片保存到相册的工具类
最新推荐文章于 2021-05-25 21:09:09 发布