Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM

前端 未结 30 2047
暗喜
暗喜 2020-11-21 07:33

I want to show the Bitmap image in ImageView from sd card which is stored already. After run my application is crash and getting Ou

相关标签:
30条回答
  • 2020-11-21 07:43

    OutOfMemoryError is the most common problem occured in android while especially dealing with bitmaps. This error is thrown by the Java Virtual Machine (JVM) when an object cannot be allocated due to lack of memory space and also, the garbage collector cannot free some space.

    As mentioned by Aleksey, you can add below entities in your manifest file android:hardwareAccelerated="false" , android:largeHeap="true" it will work for some environment's.

    <application
        android:allowBackup="true"
        android:hardwareAccelerated="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    

    you should definately read some of Androids Developer concept's, specially here:Displaying Bitmaps Efficiently

    Read all 5 topics and rewrite your code again. If it still doesn't work we will be happy to see what you've done wrong with the tutorial material.

    Here some of possible answers for these type of errors in SOF

    Android: BitmapFactory.decodeStream() out of memory with a 400KB file with 2MB free heap

    How to solve java.lang.OutOfMemoryError trouble in Android

    Android : java.lang.OutOfMemoryError

    java.lang.OutOfMemoryError

    Solution for OutOfMemoryError: bitmap size exceeds VM budget

    Edit: From the comments of @cjnash

    For anyone that still had crashes after they added this line, try sticking your image into your res/drawable-xhdpi/ folder instead of your res/drawable/ and it might resolve your issue.

    0 讨论(0)
  • 2020-11-21 07:43

    I have resolved this problem by resizing the image to lower size. I am using xamarin form. decreasing the size of the PNG image resolved the problem.

    0 讨论(0)
  • 2020-11-21 07:46

    Soluton try this in your Manifest File and use Glide Library

    compile 'com.github.bumptech.glide:glide:3.7.0'

        **Use Glide Library and Override size to less size;**
    
     if (!TextUtils.isEmpty(message.getPicture())) {
                Glide.with(mContext).load(message.getPicture())
                        .thumbnail(0.5f)
                        .crossFade()
                        .transform(new CircleTransform(mContext))
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(holder.imgProfile);
            } 
    

    android:hardwareAccelerated="false"

    android:largeHeap="true"

    <application
        android:allowBackup="true"
        android:hardwareAccelerated="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    

    Use this library

      compile 'com.github.bumptech.glide:glide:3.7.0'   
    

    Its Working Happy Coding

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapShader;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    
    import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
    import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
    
    
    public class CircleTransform extends BitmapTransformation {
        public CircleTransform(Context context) {
            super(context);
        }
    
        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            return circleCrop(pool, toTransform);
        }
    
        private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
            if (source == null) return null;
    
            int size = Math.min(source.getWidth(), source.getHeight());
            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;
    
            // TODO this could be acquired from the pool too
            Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
    
            Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
            if (result == null) {
                result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
            }
    
            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);
            float r = size / 2f;
            canvas.drawCircle(r, r, r, paint);
            return result;
        }
    
        @Override
        public String getId() {
            return getClass().getName();
        }
    }
    
    0 讨论(0)
  • 2020-11-21 07:46
    Bitmap image =((BitmapDrawable)imageView1.getDrawable()).getBitmap();
    
    ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
    
    image.compress(Bitmap.CompressFormat.JPEG,50/100,byteArrayOutputStream);
    

    50/100 if one uses 100 then original resolution can stopped the Apps for out of memory.

    if 50 or less than 100 this will be 50% or less than 100 resolution so this will prevent from out of memory problem

    0 讨论(0)
  • 2020-11-21 07:47

    You have to change the size of object in drawable. It's too big for android to display. e.g. If you are setting image try image with less pixels. It works for me. Thanks. :)

    0 讨论(0)
  • 2020-11-21 07:48

    Resize your image before setup to ImageView like this:

    Bitmap.createScaledBitmap(_yourImageBitmap, _size, _size, false);
    

    where size is actual size of ImageView. You can reach size by measuring:

    imageView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    

    and use next size imageView.getMeasuredWidth() and imageView.getMeasuredHeight() for scaling.

    0 讨论(0)
提交回复
热议问题