2010-11-24 13 views
30

Creé un mapa de bits y ahora quiero guardar ese mapa de bits en un directorio en algún lugar. ¿Alguien puede mostrarme cómo se hace esto? GraciasAndroid Guardado mapa de bits creado en el directorio de la tarjeta 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); 

(Quiero salvar _bitmapScaled a una carpeta en la tarjeta SD)

+0

'newWidth = 2592' no se echa de excepción de memoria? –

+0

@MuhammadBabar no lo haría si solo lo guarda en el disco y no lo usa en una vista de imagen –

Respuesta

101

Hola Puede escribir datos en bytes y luego crear un archivo en la carpeta sdcard con el nombre y la extensión que desee y luego escribir los bytes en ese archivo. Esto guardará bitmap en 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

¡Gracias, krunal! Esto funcionó bien para mí. – Cephron

11

También puedes probar esto.

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

Me gusta esto mejor porque no tiene que meterse con una matriz de bytes – Danation

3

Hey acaba de dar el nombre de .bmp

hacer esto:

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

sonará que soy sólo engañar alrededor pero lo intenta una vez que se obtendrá guarda en bmp foramt..Cheers

0

Esta respuesta es una actualización con un poco más de consideración para OOM y varias otras filtraciones.

Asume que tiene un directorio destinado como destino y un nombre String ya definido.

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

paso de mapa de bits en el Método saveImage, Ahorrará su mapa de bits en el nombre de un saveBitmap, dentro de la carpeta de prueba creado.

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

y utilizar esto:

saveImage(bitmap); 
Cuestiones relacionadas