public static void saveImageToGallery(Context ctx,Bitmap bm){ File dir=new File(Environment.getExternalStorageDirectory(), "newDir"); if(!dir.exists()){ dir.mkdir(); } String fileName=System.currentTimeMillis()+".jpg"; File file=new File(dir, fileName); try { FileOutputStream out=new FileOutputStream(file); bm.compress(CompressFormat.JPEG, 80, out); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { MediaStore.Images.Media.insertImage(ctx.getContentResolver(), file.getAbsolutePath(), fileName, null); } catch (FileNotFoundException e) { e.printStackTrace(); } //通知圖庫更新 ctx.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(" file://"+file.getAbsolutePath ()))); }