【问题标题】:Android Saving created bitmap to directory on sd cardAndroid将创建的位图保存到SD卡上的目录
【发布时间】:2011-05-14 21:13:40
【问题描述】:

我创建了一个位图,现在我想将该位图保存到某个目录中。谁能告诉我这是怎么做的。谢谢

FileInputStream in;
          BufferedInputStream buf;
           try {
                  in = new FileInputStream("/mnt/sdcard/dcim/Camera/2010-11-16_18-57-18_989.jpg");
                  buf = new BufferedInputStream(in);
                  Bitmap _bitmapPreScale = BitmapFactory.decodeStream(buf);
                  int oldWidth = _bitmapPreScale.getWidth();
                  int oldHeight = _bitmapPreScale.getHeight();
                  int newWidth = 2592; 
                  int newHeight = 1936;

                  float scaleWidth = ((float) newWidth) / oldWidth;
                  float scaleHeight = ((float) newHeight) / oldHeight;

                  Matrix matrix = new Matrix();
               // resize the bit map
                  matrix.postScale(scaleWidth, scaleHeight);
                  Bitmap _bitmapScaled = Bitmap.createBitmap(_bitmapPreScale, 0, 0,  oldWidth, oldHeight, matrix, true);

(我想将 _bitmapScaled 保存到我 SD 卡上的文件夹中)

【问题讨论】:

  • newWidth = 2592 不是抛出内存不足异常吗?
  • @MuhammadBabar 如果他只是将其保存到磁盘并且不在图像视图中使用它,则不会

标签: android image save


【解决方案1】:

嗨,您可以将数据写入字节,然后在 sdcard 文件夹中创建一个具有您想要的任何名称和扩展名的文件,然后将字节写入该文件。 这会将位图保存到 sdcard。

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
_bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "test.jpg");
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());

// remember close de FileOutput
fo.close();

【讨论】:

    【解决方案2】:

    你也可以试试这个。

    File file = new File(strDirectoy,imgname);
    OutputStream fOut = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
    fOut.flush();
    fOut.close();
    MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
    

    【讨论】:

    • 我更喜欢这个,因为你不必弄乱字节数组
    • @Andro Selva MediaStore.Images.Media.insertImage 真的需要吗?因为没有它,代码可以工作。
    • 嗨。我在bitmap 上得到未解决的参考。我应该用什么替换bitmap
    【解决方案3】:

    _bitmapScaled.compress() 应该可以解决问题。查看文档:http://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream)

    【讨论】:

      【解决方案4】:

      只需将扩展名更改为 .bmp

      这样做:

      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
      _bitmapScaled.compress(Bitmap.CompressFormat.PNG, 40, bytes);
      
      //you can create a new file name "test.BMP" in sdcard folder.
      File f = new File(Environment.getExternalStorageDirectory()
                              + File.separator + "test.bmp")
      

      听起来我只是在胡闹,但尝试一次,它会以BMP 格式保存。干杯!

      【讨论】:

        【解决方案5】:

        此答案是对 OOM 和其他各种泄漏的更多考虑的更新。

        假设您有一个作为目标的目录和一个已经定义的名称字符串。

            File destination = new File(directory.getPath() + File.separatorChar + filename);
        
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            source.compress(Bitmap.CompressFormat.PNG, 100, bytes);
        
            FileOutputStream fo = null;
            try {
                destination.createNewFile();
        
                fo = new FileOutputStream(destination);
                fo.write(bytes.toByteArray());
            } catch (IOException e) {
        
            } finally {
                try {
                    fo.close();
                } catch (IOException e) {}
            }
        

        【讨论】:

          【解决方案6】:

          将位图传递给 saveImage 方法,它将以 saveBitmap 的名称保存您的位图,在创建的测试文件夹中。

          private void saveImage(Bitmap data) {
                              File createFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"test");
                              if(!createFolder.exists())
                              createFolder.mkdir();
                              File saveImage = new File(createFolder,"saveBitmap.jpg");
                              try {
                                  OutputStream outputStream = new FileOutputStream(saveImage);
                                  data.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
                                  outputStream.flush();
                                  outputStream.close();
                              } catch (FileNotFoundException e) {
                                  e.printStackTrace();
                              } catch (IOException e) {
                                  e.printStackTrace();
                              }
                          }
          

          并使用它:

            saveImage(bitmap);
          

          【讨论】:

            猜你喜欢
            • 2012-08-04
            • 2012-10-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多