如何保存一个位图到手机的画廊? [英] How to save a bitmap into phone's gallery?

查看:111
本文介绍了如何保存一个位图到手机的画廊?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拍一张照片,并显示在一个ImageView的。然后,我从我的活动ImageView的位图,当我preSS一个按钮,我想保存该位图到手机的画廊。我应该怎么办?

I take a photo and show it in an imageview. Then I get the bitmap from the imageview in my activity and when I press a button I want to save this bitmap into phone's gallery. What should I do?

推荐答案

调用这个函数在按钮的onClick

call this function in 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();
  }
}

检查此保存位图

这篇关于如何保存一个位图到手机的画廊?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆