如何将位图保存到手机相册?

4

我拍了一张照片并在ImageView中展示它。然后,我从活动中获取ImageView中的位图,当我按下按钮时,我想将此位图保存到手机相册中。我该怎么办?

3个回答

8

在按钮的 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();
  }
}

查看这个保存位图


1

试试这个

        Bitmap toDisk = Bitmap.createBitmap(w1,h1,Bitmap.Config.ARGB_8888); 
        setBitmap(toDisk); 
        Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ban_background);
        Bitmap resizeImage1=Bitmap.createScaledBitmap(myBitmap,590,350,false);
          try {
                    toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/mnt/sdcard/image".jpg")));

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

7
对于未记录、格式不良、没有解释的仅包含代码的答案,给予-1分。 - WarrenFaith

0
你需要先获取手机外部存储目录的路径,然后获取图库图片存储的目录。在大多数安卓机型上,这个目录为/mnt/sdcard/pictures,但我不建议硬编码这个路径,而是使用。
Environment.getExternalStorageDirectory(); 

接下来只需创建一个文件路径到该目录,并使用 outputStream 将位图写入该目录。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接