2010-02-19 9 views
5

Necesito tomar una foto con la cámara y, dependiendo del tamaño de la imagen, rotarla antes de guardarla en la galería.Cómo trabajar con mapa de bits grande. Girar e insertar en la galería

estoy usando

Intención imageCaptureIntent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE); imageCaptureIntent.putExtra (MediaStore.EXTRA_OUTPUT, uri); startActivityForResult (imageCaptureIntent, IMAGE_CAPTURE);

Para tomar la foto y guardarla en un archivo temporal.

Entonces

Bitmap bmp = BitmapFactory.decodeFile (imagePath);
String str = android.provider.MediaStore.Images.Media.insertImage (cr, bmp, name, description);

Para guardarlo.

Este es el código i intentado utilizar para hacer girar el mapa de bits

matriz Matrix = new Matrix();
matrix.postRotate (180);
Bitmap x = Bitmap.createBitmap (bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matriz, verdadero);
android.provider.MediaStore.Images.Media.insertImage (cr, x, name, description);

El problema es que recibo una OutOfMemoryException.

¿Hay una mejor manera de manejar los mapas de bits para evitar romper la memoria?

~ Gracias de antemano, que se refiere a

Respuesta

2

no creo que hay una mejor manera de manejar los mapas de bits. Podría tratar de analizar datos directamente desde un archivo como Byte [] una parte a la vez, y manipularlos en pedazos; es difícil y probablemente termines con un código muy feo.

también sugieren lo siguiente:

  • Uso android.provider.MediaStore.Images.Media.insertImage(cr, imagePath, name, description) en lugar de android.provider.MediaStore.Images.Media.insertImage(cr, bmp, name, description) esta manera no hay necesidad de llamar a Bitmap bmp = BitmapFactory.decodeFile(imagePath) y ningún mapa de bits se carga en memoria en ese punto.

  • A lo largo de su código, asegúrese de que no se cargue ningún mapa de bits a menos que sea necesario. Establezca mapas de bits que ya no sean necesarios para null y llame al recolector de elementos no utilizados, o use bmp.recycle().

+0

bmp.recycle() no ayuda aquí porque realmente puede hacerlo bien DESPUÉS de atrapar OOM. Quiero decir después de rotar bitmap y guardarlo. Sin embargo, la OOM probablemente llegue en el momento de rotación ya que tiene que mantener 2 mapas de bits iguales en la memoria RAM allí. – Stan

0

Tuve el mismo problema con la rotación del mapa de bits. El problema aquí:

Bitmap bmp = BitmapFactory.decodeFile(imagePath); //this is the image you want to rotate 
    // keeping in mind that you want to rotate the whole original image instead 
    // of its downscaled copy you cant use BitmapFactory downscaling ratio 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(180); 
    Bitmap x = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); 
    // the line above creates another bitmap so we have here 2 same sized bitmaps 
    // even using the same var (bmp instead of x) wont change anything here 
    // so you gonna get the OOM here 

es que crea 2 mapas de bits por lo que quieren x2 más memoria RAM.
Verificar my question and solution here. Yo apuesto por ImageMagick lib.

Cuestiones relacionadas