Android把图片写入到图库中,用户立即可以看到 发表于 2016-04-25 | 分类于 Android 参考:把图片写入图库中 123456789101112131415161718192021222324252627282930public static void saveImageToGallery(Context context, Bitmap bmp) { // 首先保存图片到SD卡的任意目录 File appDir = new File(Environment.getExternalStorageDirectory(), "CustomImages"); if (!appDir.exists()) { appDir.mkdir(); } //图片名称可以任意设置,此处设置为当前时间 String fileName = System.currentTimeMillis() + ".jpg"; File file = new File(appDir, fileName); try { FileOutputStream fos = new FileOutputStream(file); bmp.compress(CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 其次把保存在SD目录中的文件插入到系统图库中 try { MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null); } catch (FileNotFoundException e) { e.printStackTrace(); } // 最后通知图库更新 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));}