2016-10-02 7 views
0

Ich bin neu zu Xamarin Android und für unten Code erhalten OutOfMemoryException. Hier ist profilebitMap ein Bitmap und mProfileImage ist ein ImageView. Ich habe versucht, dies mit block-und dispose/recycle-Methoden auch, aber immer noch den gleichen Fehler nach mehreren kehrt auf die Bildseite. Bitte hilf mir dabei.OutOfMemoryError in meinem Code

if (!string.IsNullOrEmpty(profile.Image)) 
{ 
    string[] fileExtension = profile.Image.Split('/'); 
    string _imagePath = System.IO.Path.Combine(_documentsPath.ToString(), profile.ID + fileExtension[fileExtension.Length - 1]); 

    if (File.Exists(_imagePath)) 
    { 
     profilebitMap = await BitmapFactory.DecodeFileAsync(_imagePath); 
     //profilebitMap = Util.Base64ToBitmap(appPreferencce.getAccessKey(profile.Image)); 
    } 
    else 
    { 
     profilebitMap = Util.GetImageBitmapFromUrl(profile.Image, appPreferencce.getAccessKey("username"), appPreferencce.getAccessKey("password")); 
     using (var stream = new MemoryStream()) 
     { 
      profilebitMap.Compress(Bitmap.CompressFormat.Png, 100, stream); 
      var imageBytes = stream.ToArray(); 
      File.WriteAllBytes(_imagePath, imageBytes); 
     } 
    } 
} 
else 
{ 
    profilebitMap = BitmapFactory.DecodeResource(this.ApplicationContext.Resources, Resource.Drawable.dummyuser); 
} 
CircularDrawable d = new CircularDrawable(profilebitMap, 
        (int)Util.ConvertDpToPx(ApplicationContext, margin), 
        Util.ConvertDpToPx(ApplicationContext, strokeWidth), 
        new Android.Graphics.Color(ContextCompat.GetColor(this, Resource.Color.normal3))); 
mProfileImage.SetBackgroundDrawable(d); 

Antwort

0

Sie müssen das Rad nicht neu erfinden, können Sie Bibliotheken verwenden, die Sie Bilder in wenigen Zeilen zu laden. Sie sollten einen Blick auf die beeindruckende Bibliothek Picasso werfen.

Bye

0

die OutOfMemoryException Um zu vermeiden, wir BitmapFactory.Options Klasse verwenden sollten.

BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(getResources(), R.id.myimage, options); 
    int imageHeight = options.outHeight; 
    int imageWidth = options.outWidth; 
    String imageType = options.outMimeType; 

Einstellung der inJustDecodeBounds Eigenschaft auf true während Dekodierung Speicherzuweisung vermeidet, für das Bitmap-Objekt null zurückkehrt aber outWidth, outHeight und outMimeType Einstellung.

Nun, da die Bildabmessungen bekannt sind, können sie verwendet werden, um zu entscheiden, ob das vollständige Bild in den Speicher geladen werden soll oder ob stattdessen eine unterabgetastete Version geladen werden soll.

Zum Beispiel erzeugt ein Bild mit der Auflösung 2048x1536, das mit einer inSampleSize von 4 decodiert wird, eine Bitmap von ungefähr 512x384. Das Laden in den Speicher verwendet 0,75 MB anstelle von 12 MB für das vollständige Bild.

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

Bitte folgen Sie diese URL https://developer.android.com/training/displaying-bitmaps/load-bitmap.html