如何将位图保存到手机的图库?

gul*_*sen 4 android bitmap android-gallery

我拍了一张照片并在imageview中显示.然后我在我的活动中从imageview获取位图,当我按下按钮时我想将这个位图保存到手机的图库中.我该怎么办?

Raj*_*ddy 7

在Button onClick中调用此函数

private void saveImage() {

  File myDir=new File("/sdcard/saved_images");
  myDir.mkdirs();
  Random generator = new Random();
  int n = 10000;
  n = generator.nextInt(n);
  String fname = "Image-"+ n +".jpg";
  File file = new File (myDir, fname);
  if (file.exists ()) file.delete (); 
  try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

   } catch (Exception e) {
       e.printStackTrace();
  }
}
Run Code Online (Sandbox Code Playgroud)

检查这个保存位图