2016-06-27 18 views
5

Ho un metodo per ruotare le immagini, ma ho sempre ricevuto un OutMemoryError, ma le mie immagini nella galleria iare preso dalla macchina fotografica e la larghezza taglia è 5000 ~ dal telefonoAndroid immagine ridimensionare memoria errori

ho ridimensionato foto alla larghezza 1280 e l'altezza 960

mio primo metodo per l'esposizione e l'immagine ridimensionare è

public static Boolean ShowImagesCapture(Context context, Uri PATH_IMAGE, ImageCropView view,int width, int height){ 

    int orientation=0; 
    Boolean success = true; 
    try { 
     Bitmap bitmap =null; 

     if (Build.VERSION.SDK_INT < 19) { 
      String selectedImagePath = getPath(PATH_IMAGE,context); 
      bitmap = BitmapFactory.decodeFile(selectedImagePath); 
      orientation=GetPhotoOrientation(context,getRealPathFromURI(context,PATH_IMAGE)); 
     } 

     else { 
      ParcelFileDescriptor parcelFileDescriptor; 

      try { 
       parcelFileDescriptor = context.getContentResolver().openFileDescriptor(PATH_IMAGE, "r"); 
       FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); 
       bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor); 
       parcelFileDescriptor.close(); 
       orientation=GetPhotoOrientation(context,getRealPathFromURI(context,PATH_IMAGE)); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
     switch (orientation) { 

      case ExifInterface.ORIENTATION_ROTATE_180: 
       bitmap=rotateBitmap(bitmap,3,width,height); 
       view.setImageBitmap(bitmap); 

       break; 
     break; 
      case ExifInterface.ORIENTATION_ROTATE_90: 
       bitmap=rotateBitmap(bitmap,8,width,height); 
       view.setImageBitmap(bitmap); 
       break; 

      case ExifInterface.ORIENTATION_TRANSVERSE: 
       break; 

      case ExifInterface.ORIENTATION_ROTATE_270: 
       bitmap=rotateBitmap(bitmap,6,width,height); 
       view.setImageBitmap(bitmap); 
       break; 

      default: 
       view.setImageBitmap(bitmap); 

     } 

     bitmap = null; 

    } 
    catch (Exception e) { 
     e.printStackTrace(); 
     success= false; 
    } 
    System.gc(); 
    return success; 
} 

e il mio metodo per la rotazione dell'immagine è

public static Bitmap rotateBitmap(Bitmap bitmap, int orientation,int width,int height) { 

    try { 
     Matrix matrix = new Matrix(); 
     switch (orientation) { 
      case ExifInterface.ORIENTATION_NORMAL: 
       return bitmap; 
      case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: 
       //    matrix.setScale(-1, 1); 
       break; 
      case ExifInterface.ORIENTATION_ROTATE_180: 
       matrix.setRotate(180); 
       break; 
      case ExifInterface.ORIENTATION_FLIP_VERTICAL: 
       matrix.setRotate(180); 
       //    matrix.postScale(-1, 1); 
       break; 
      case ExifInterface.ORIENTATION_TRANSPOSE: 
       matrix.setRotate(90); 
       //    matrix.postScale(-1, 1); 
       break; 
      case ExifInterface.ORIENTATION_ROTATE_90: 
       matrix.setRotate(90); 
       break; 
      case ExifInterface.ORIENTATION_TRANSVERSE: 
       matrix.setRotate(-90); 
       //    matrix.postScale(-1, 1); 
       break; 
      case ExifInterface.ORIENTATION_ROTATE_270: 
       matrix.setRotate(-270); 
       break; 
      default: 
       return bitmap; 
     } 



     Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false); 
     bitmap.recycle(); 
     return bmRotated; 

    } 
    catch (OutOfMemoryError e) { 
     Log.e(TAG,"Out memory Error"); 
     return null; 
    }catch (Exception e){ 
     e.printStackTrace(); 
     return null; 
    } 
} 

dov'è il mio errore?

* ------------------- ** AGGIORNAMENTO 27 GIUGNO 2016 ** ------------------ - *

MIO CODICE HANNO uN MIGLIORE vERSIONE funzionare bene

public static Bitmap rotateBitmap(Bitmap bitmap, int orientation,int width,int height) { 

    try { 
     Matrix matrix = new Matrix(); 
     switch (orientation) { 
      case ExifInterface.ORIENTATION_NORMAL: 
       return bitmap; 
      case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: 
       matrix.setScale(-1, 1); 
       break; 
      case ExifInterface.ORIENTATION_ROTATE_180: 
       matrix.setRotate(180); 
       break; 
      case ExifInterface.ORIENTATION_FLIP_VERTICAL: 
       matrix.setRotate(180); 
       matrix.postScale(-1, 1); 
       break; 
      case ExifInterface.ORIENTATION_TRANSPOSE: 
       matrix.setRotate(90); 
       matrix.postScale(-1, 1); 
       break; 
      case ExifInterface.ORIENTATION_ROTATE_90: 
       matrix.setRotate(90); 
       break; 
      case ExifInterface.ORIENTATION_TRANSVERSE: 
       matrix.setRotate(-90); 
       matrix.postScale(-1, 1); 
       break; 
      case ExifInterface.ORIENTATION_ROTATE_270: 
       matrix.setRotate(-270); 
       break; 
      default: 
       return bitmap; 
     } 
     Bitmap bmRotated= null; 
     try { 
      Bitmap tmp_bitmap= Bitmap.createScaledBitmap(bitmap,width,height,true); 

      bmRotated = Bitmap.createBitmap(tmp_bitmap, 0, 0, tmp_bitmap.getWidth(),tmp_bitmap.getHeight(), matrix, true); 

      bitmap.recycle(); 
     }catch (OutOfMemoryError e){ 
      e.printStackTrace(); 
     } 
     return bmRotated; 

    } catch (Exception e){ 
     e.printStackTrace(); 
     return null; 
    } 
} 


public static Bitmap decodefilebitmap(String selectedImagePath, int reqWidth, int reqHeight) { 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(selectedImagePath, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeFile(selectedImagePath, options); 
} 

public static int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) > reqHeight 
       && (halfWidth/inSampleSize) > reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 

//METODO PARA MOSTRAR LA IMAGEN DESDE LA GALERIA 
public static Boolean ShowImagesCapture(Context context, Uri PATH_IMAGE, ImageCropView view,int width, int height){ 

    int orientation=0; 
    Boolean success = true; 
    try { 
     Bitmap bitmap =null; 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 

     if (Build.VERSION.SDK_INT < 19) { 
      String selectedImagePath = getPath(PATH_IMAGE,context); 
      bitmap = decodefilebitmap(selectedImagePath,bitmap.getWidth(),bitmap.getHeight()); 
      orientation=GetPhotoOrientation(context,getRealPathFromURI(context,PATH_IMAGE)); 
     } 

     else { 
      ParcelFileDescriptor parcelFileDescriptor; 

      try { 
       parcelFileDescriptor = context.getContentResolver().openFileDescriptor(PATH_IMAGE, "r"); 
       FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); 
       bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor); 
       parcelFileDescriptor.close(); 
       orientation=GetPhotoOrientation(context,getRealPathFromURI(context,PATH_IMAGE)); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
     switch (orientation) { 


      case ExifInterface.ORIENTATION_ROTATE_180: 
       bitmap=rotateBitmap(bitmap,3,width,height); 
       view.setImageBitmap(bitmap); 

       break; 

      case ExifInterface.ORIENTATION_ROTATE_90: 
       bitmap=rotateBitmap(bitmap,8,width,height); 
       view.setImageBitmap(bitmap); 
       break; 

      case ExifInterface.ORIENTATION_TRANSVERSE: 
       break; 

      case ExifInterface.ORIENTATION_ROTATE_270: 
       bitmap=rotateBitmap(bitmap,6,width,height); 
       view.setImageBitmap(bitmap); 
       break; 

      default: 
       view.setImageBitmap(bitmap); 

     } 

     bitmap = null; 

    } 
    catch (Exception e) { 
     e.printStackTrace(); 
     success= false; 
    } 
    System.gc(); 
    return success; 
} 

risposta

3

Questo perché si sta caricando l'intera bitmap in memoria bitmap = BitmapFactory.decodeFile(selectedImagePath); e poi mostrando una versione ridimensionata in ImageView (ma sprecare memoria perché si ha il pieno versione in formato RAM). Devi caricare una versione ridotta. Non è lo stesso caricare tutta la bitmap e quindi eseguire operazioni su di essa (ridimensionare, ruotare, metterlo in una vista immagine) piuttosto che caricare una versione ridotta di quella bitmap in memoria. Ad esempio, se si dispone di un'immagine di 5000 per 5000 pixel, si supponga che in formato JPEG la sua dimensione sia di circa 1 MB. Ma quando lo si carica in memoria lo si decomprime e si carica l'intera versione non compressa di quell'immagine. Supponete di caricarlo a 32 bit per pixel, quindi la sua dimensione in RAM sarà di 5000x5000x32 bit, ovvero circa 95 MB! Quindi è necessario caricare una versione ridotta. Dai un'occhiata a questo documento degli sviluppatori Android about loading a scaled down bitmap version into memory. Questo ti aiuterà a capire meglio il problema. Potresti anche utilizzare librerie di caricamento immagini come Glide. Queste librerie fanno tutto questo e altro.

+0

metodo yes and .. ruota l'immagine per prima ridimensionare l'immagine per la larghezza e l'altezza dell'immagine bassa e dopo aver ruotato l'immagine grazie! – pedroooo

+0

aggiorno alla soluzione per il codice !! saluti – pedroooo

+0

@pedroooo Nessun problema, felice di averti aiutato =). – josemgu91

Problemi correlati