public class SaveBitmapToPhoto {
/**
* 保存图片到指定路径
*
* @param context
* @param bitmap 要保存的图片
* @param fileName 自定义图片名称 getString(R.string.app_name) + "" + System.currentTimeMillis()+".png"
* @return true 成功 false失败
*/
public static boolean saveImageToGallery(Context context, Bitmap bitmap, String fileName) {
// 保存图片至指定路径
String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "qrcode";
File appDir = new File(storePath);
if (!appDir.exists()) {
appDir.mkdir();
}
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
//通过io流的方式来压缩保存图片(80代表压缩20%)
boolean isSuccess = bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
fos.flush();
fos.close();
//发送广播通知系统图库刷新数据
Uri uri = Uri.fromFile(file);
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
if (isSuccess) {
return true;
} else {
return false;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
网友评论