android – ImageView OutofMemoryException

ImageView中插入图像时遇到问题.我已经尝试了几种方法来解决它,但它仍然失败,最新版本如下.图像大小为3000×3000.

码:

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 

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

解决方法:

位图的最大大小为2048×2048,如果您的位图大于此值,则应缩放它.
我正在使用此方法来缩放位图:

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

您还可以使用以下方法检查内存状态:

ActivityManager actMgr = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo minfo = new ActivityManager.MemoryInfo();
actMgr.getMemoryInfo(minfo);
if(minfo.lowMemory) { //do something}
上一篇:android – ImageView在布局中显示但不在实际设备上显示


下一篇:如何在android中压缩imageview的图像