Exception : OutOfMemoryError

前端 未结 7 896
北海茫月
北海茫月 2020-11-29 13:49

I have published my app in play store. Now In Crashes and ANRs I am getting following errors on 2 devices (Galaxy Note3 and Galaxy Note II). I dont know how to solve these e

7条回答
  •  再見小時候
    2020-11-29 14:29

    Do not load Bitmap directly if you are not sure about the size of the image. Sample it when the size is too large. At most time, the length of an image loaded should not exceed the length of your phone screen. Use code like this to sample image.

    public static Bitmap loadImage(Context cx, Uri uri, int maxLength) {
        InputStream is = cx.getContentResolver().openInputStream(uri);
        Options opts = new Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, opts);
        int length = Math.max(opts.outWidth, opts.outHeight);
        int n = 1;
        while (length > maxLength) {
            maxLength /= 2;
            n ++;
        }
        is = cx.getContentResolver().openInputStream(uri);
        opts.inJustDecodeBounds = false;
        opts.inPurgeable = true;
        opts.inDither = true;
        opts.inPurgeable = true;
        opts.inSampleSize = sample;
        Bitmap bm = BitmapFactory.decodeStream(is, null, opts);
        return bm;
    }
    

提交回复
热议问题