【问题标题】:How to save a bitmap into phone's gallery? [closed]如何将位图保存到手机的图库中? [关闭]
【发布时间】:2011-12-31 02:58:25
【问题描述】:

我拍照并在图像视图中显示。然后我从我的活动中的图像视图中获取位图,当我按下按钮时,我想将此位图保存到手机的图库中。我该怎么办?

【问题讨论】:

    标签: android bitmap android-gallery


    【解决方案1】:

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

    检查这个save bitmap

    【讨论】:

      【解决方案2】:

      试试这个

              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();
                      }
      

      【讨论】:

      • -1 表示未记录、格式错误、无法解释的纯代码答案。
      【解决方案3】:

      您必须首先获取手机外部存储目录的路径,然后是存储画廊图像的目录。在大多数 Android 型号上,这是 /mnt/sdcard/pictures 但我不建议硬编码此路径,而是使用

      Environment.getExternalStorageDirectory(); 
      

      之后,只需创建该目录的文件路径并使用 outputStream 将您的位图写入该目录。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-27
        • 2018-05-29
        • 1970-01-01
        • 1970-01-01
        • 2013-07-18
        • 1970-01-01
        • 2018-09-08
        相关资源
        最近更新 更多