2010-11-24 205 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);