public class PhotoUtils { private static String mPhotoPath; private static File mPhotoFile; private static Intent intent; // 将图片保存到相册 public static void saveImageToGallery(Context context, Bitmap bmp) { //设置图片文件路径,getSDPath()和getPhotoFileName()具体实现在下面 mPhotoPath = getSDPath()+"/"+ getPhotoFileName(); // 首先保存图片 mPhotoFile = new File(mPhotoPath); if (!mPhotoFile.exists()) { try { mPhotoFile.createNewFile();//创建新文件 } catch (IOException e) { e.printStackTrace(); } } try { FileOutputStream fos = new FileOutputStream(mPhotoFile); bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 最后通知图库更新 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(mPhotoFile))); } public static String getSDPath(){ File sdDir = null; boolean sdCardExist = Environment.getExternalStorageState() .equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在 if (sdCardExist){ sdDir = Environment.getExternalStorageDirectory();//获取跟目录 } return sdDir.toString(); } private static String getPhotoFileName() { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat dateFormat = new SimpleDateFormat( "'IMG'_yyyyMMdd_HHmmss"); return dateFormat.format(date) +".jpg"; } }