2010-11-24 185 views
30

我创建了一个位图,现在我想将该位图保存到某个目录。任何人都可以告诉我这是如何完成的。由于Android将创建的位图保存到SD卡上的目录

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卡上)

+0

`newWidth = 2592`没有把它抛出内存异常? – 2014-10-26 13:47:14

+0

@MuhammadBabar它不会,如果他只是将其保存到磁盘,并不使用它在imageview – 2016-02-18 13:29:34

回答

101

您可以将数据写入字节,然后使用任何名称和扩展名在SD卡文件夹中创建一个文件,然后将字节写入该文件。 这会将位图保存到SD卡。

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(); 
+1

谢谢,krunal!这对我很好。 – Cephron 2011-03-23 18:32:03

11

你也可以试试这个。

OutputStream fOut = null; 
         File file = new File(strDirectoy,imgname); 
          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()); 
3

嘿只是给名称.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 foramt..Cheers

0

这个答案是一个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) {} 
    } 
0

将位图传递给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);