在我的代码中获取OutOfMemoryError

时间:2016-10-02 07:19:42

标签: c# android xamarin.android out-of-memory

我是Xamarin android的新手,下面的代码是OutOfMemoryException。此处profilebitMapBitmapmProfileImageImageView。我已尝试使用块和dispose / recycle方法,但在多次返回图像页面后仍然得到相同的错误。请帮助我。

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

2 个答案:

答案 0 :(得分:0)

您不必重新发明轮子,您可以使用以几行加载图片的库。你应该看一下很棒的库Picasso

再见

答案 1 :(得分:0)

为了避免OutOfMemoryException,我们应该使用BitmapFactory.Options类。

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;

解码时将inJustDecodeBounds属性设置为true可避免内存分配,为位图对象返回null但设置outWidth,outHeight和outMimeType。

现在已知图像尺寸,它们可用于决定是否应将完整图像加载到内存中,或者是否应加载子采样版本。

例如,使用inSampleSize为4解码的分辨率为2048x1536的图像会产生大约512x384的位图。将其加载到内存中对于完整图像使用0.75MB而不是12MB。

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

请关注此网址https://developer.android.com/training/displaying-bitmaps/load-bitmap.html