直接上代码,复制粘贴吧!
public static void saveImageToGallery(Context context, Bitmap bmp) { if (bmp == null){ return; } // 首先保存图片 File appDir = new File(Environment.getExternalStorageDirectory(), "wyx"); //这里可以改目录 if (!appDir.exists()) { appDir.mkdir(); } String fileName = System.currentTimeMillis() + ".png"; File file = new File(appDir, fileName); try { FileOutputStream fos = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }catch (Exception e){ 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); }