2010-11-24 14 views
30

Bir bitmap oluşturdum ve şimdi o bitmapi bir yere bir dizine kaydetmek istiyorum. Herkes bana bunun nasıl yapıldığını gösterebilir. Android Kaydetme, sd kartındaki dizine bitmap oluşturdu

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

Teşekkür

+0

'newWidth = 2592' bellek istisna dışında atar ki? –

+0

@MuhammadBabar o sadece diske kaydeder ve bir imageview –

cevap

101

Merhaba Baytlara veri yazabilir ve daha sonra istediğiniz ad ve uzantıya sahip sdcard klasöründe bir dosya oluşturabilir ve daha sonra bu dosyaya bayt yazabilirsiniz. Bu, bitmap'i sdcard'a kaydeder.

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

içinde kullanmazsa olmaz Teşekkür ederim, krunal! Bu benim için iyi çalıştı. – Cephron

11

Bunu da deneyebilirsiniz.

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

Bunu daha iyi seviyorum çünkü bir bayt dizisiyle uğraşmanıza gerek yok – Danation

3

Hey sadece bu Do .bmp

isim vermek:

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

o IM SADECE eğlenmeyi olduğunu ses ama kaydedilen alırsınız kez deneyeceğim bmp foramt..Cheers

0

Bu cevap, OOM ve diğer çeşitli sızıntılar için biraz daha düşünülmüş bir güncellemedir.

Hedef olarak tasarlanan bir dizinin ve önceden tanımlanmış bir ad dizininiz olduğunu varsayar. saveimage Yöntemine

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

Geçiş bitmap, Oluşturulan deney klasörünün içindeki bir saveBitmap adına sizin bitmap kurtaracak.

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

ve bu kullanın:

saveImage(bitmap); 
İlgili konular