1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| 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; } }
|