ImageView OutofMemoryException

2019-01-25 23:52发布

问题:

I have a problem when inserting an image in an ImageView. I have tried several ways to solve it but it still fails, the latest version follows. The image size is 3000x3000.

Code:

ImageView iv = (ImageView) findViewById(R.id.imageView);  
Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.prueba_plano_2).copy(Bitmap.Config.ARGB_8888, true);
iv.setImageBitmap(originalBitmap);

XML:

 <FrameLayout
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/buttonlayer" >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:contentDescription="@string/desc"
            android:scaleType="matrix" />
</FrameLayout>

Logcat:

06-21 09:32:06.721: E/AndroidRuntime(25709): FATAL EXCEPTION: main
06-21 09:32:06.721: E/AndroidRuntime(25709): java.lang.OutOfMemoryError
06-21 09:32:06.721: E/AndroidRuntime(25709):    at android.graphics.Bitmap.nativeCreate(Native Method)
06-21 09:32:06.721: E/AndroidRuntime(25709):    at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
06-21 09:32:06.721: E/AndroidRuntime(25709):    at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
06-21 09:32:06.721: E/AndroidRuntime(25709):    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
06-21 09:32:06.721: E/AndroidRuntime(25709):    at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
06-21 09:32:06.721: E/AndroidRuntime(25709):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
06-21 09:32:06.721: E/AndroidRuntime(25709):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
06-21 09:32:06.721: E/AndroidRuntime(25709):    at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:374)
06-21 09:32:06.721: E/AndroidRuntime(25709):    at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:404)
06-21 09:32:06.721: E/AndroidRuntime(25709):    at 

.............

回答1:

The max size of a bitmap is 2048x2048, if your bitmap is bigger than this you should scale it..
I'm using this method to scale a bitmap:

public static Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight) {   
    if(bitmapToScale == null)
        return null;
    //get the original width and height
    int width = bitmapToScale.getWidth();
    int height = bitmapToScale.getHeight();
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();

    // resize the bit map
    matrix.postScale(newWidth / width, newHeight / height);

    // recreate the new Bitmap and set it back
    return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, true);
} 

You can also check the memory state using this:

ActivityManager actMgr = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo minfo = new ActivityManager.MemoryInfo();
actMgr.getMemoryInfo(minfo);
if(minfo.lowMemory) { //do something}


回答2:

You can also load the image dimension without loading the complete image.

android.graphics.BitmapFactory.Options options = new
    android.graphics.BitmapFactory.Options();

options.inJustDecodeBounds = true;

android.graphics.BitmapFactory.decodeFile(filepath, options);

Now you have the image dimension available via:

options.outHeight
options.outWidth


回答3:

Try to read the official Android Developer docs:Loading Large Bitmaps Efficiently in this link,Given that you are working with limited memory (mobile phones), ideally you only have to load a lower resolution version in memory. Follow the advices posted in the link and you will avoid your OM problem