0

Dans mon application, j'utilise une base de données SQLite pour stocker les données d'un grand nombre d'images. Certaines images peuvent atteindre 600 x 600 pixels. J'utilise une liste personnalisée pour créer les bitmaps. Je sais qu'il existe une méthode bitmap.recycle(); mais je ne suis pas sûr de savoir comment l'utiliser avec un listview.OutOfMemoryError android Bitmap listview

+0

double possible de [étrange de problème de mémoire lors du chargement d'une image à un objet Bitmap] (http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading -an-image-to-a-bitmap-object) – tyczj

Répondre

0

Voici la solution pour gérer bitmaps dans Android

D'abord, vous devez calculer le sampleBitmapSize du bitmap (pour charger la version inférieure du même bitmap)

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
     int reqWidth, int reqHeight) { 

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

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

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeResource(res, resId, options); 
} 

est inférieure à la fonction util pour calculer InSampleSize (un entier qui définit la valeur (rating) de la qualité d'un bitmap).

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

    Log.i("ImageUtil", "InSampleSize: "+inSampleSize); 
    return inSampleSize; 
} 
+0

en utilisant cette technique je peux manipuler l'image de pixel 25kx25k facilement sur un fil non-ui. –

+0

Merci, et dans les paramètres reqWidth et reqHeight je devrais mettre les tailles nécessaires telles que 600x600? –

+0

Oui, c'est la hauteur et la largeur de l'image que vous voulez avoir (la plus petite version de l'image originale). –

Questions connexes