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

(私は私のSDカード上のフォルダに_bitmapScaled保存したい)

+0

'newWidth = 2592'それはメモリ不足の例外スローしませんでしたか? –

+0

@MuhammadBabarディスクに保存してイメージビューで使用しないとしないと、 –

答えて

101

こんにちはあなたはバイトにデータを書き込んでから、必要な名前と拡張子を持つ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(); 
+1

ありがとう、krunal!これは私のためにうまくいった。 – Cephron

11

これを試すこともできます。それはIMはJUST浮気ことに聞こえるが、それは中に保存されますよ一度それを試してみましょう

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") 

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

バイト配列を混乱させる必要がないので、私はこれがより好きです – Danation

3

ちょっとだけ.bmpの

に名前を付け、これを行いますbmp foramt..Cheers

0

この回答は、OOMやその他のさまざまなリークを考慮したアップデートです。

宛先として意図されたディレクトリと既に定義されている名前Stringがあるとします。 saveImageメソッドへ

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

パスビットマップは、それが作成したテストフォルダ内に、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); 
関連する問題