ImageView的OutOfMemoryException异常 [英] ImageView OutofMemoryException

查看:103
本文介绍了ImageView的OutOfMemoryException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题在ImageView的插入图像时。我曾尝试多种方法来解决它,但它仍然失败,最新的版本如下。影像尺寸为3000x3000。

code:

  ImageView的IV =(ImageView的)findViewById(R.id.imageView);
位图originalBitmap = BitmapFactory.de codeResource(getResources(),R.drawable.prueba_plano_2).copy(Bitmap.Config.ARGB_8888,真正的);
iv.setImageBitmap(originalBitmap);
 

XML:

 <的FrameLayout
        机器人:ID =@ + ID /根
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_alignParentBottom =真
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID / buttonlayer>

        < ImageView的
            机器人:ID =@ + ID / ImageView的
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:layout_gravity =中心
            机器人:adjustViewBounds =真
            机器人:contentDescription =@字符串/递减
            机器人:scaleType =矩阵/>
< /的FrameLayout>
 

logcat的:

 九月六日至21日:32:06.721:E / AndroidRuntime(25709):致命异常:主要
九月六号至21日:32:06.721:E / AndroidRuntime(25709):java.lang.OutOfMemoryError
九月六号至21日:32:06.721:E / AndroidRuntime(25709):在android.graphics.Bitmap.nativeCreate(本机方法)
九月六号至21日:32:06.721:E / AndroidRuntime(25709):在android.graphics.Bitmap.createBitmap(Bitmap.java:605)
九月六号至21日:32:06.721:E / AndroidRuntime(25709):在android.graphics.Bitmap.createBitmap(Bitmap.java:551)
九月六号至21日:32:06.721:E / AndroidRuntime(25709):在android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
九月六号至21日:32:06.721:E / AndroidRuntime(25709):在android.graphics.BitmapFactory.finishDe code(BitmapFactory.java:524)
九月六号至21日:32:06.721:E / AndroidRuntime(25709):在android.graphics.BitmapFactory.de codeStream(BitmapFactory.java:499)
九月六号至21日:32:06.721:E / AndroidRuntime(25709):在android.graphics.BitmapFactory.de codeResourceStream(BitmapFactory.java:351)
九月六号至21日:32:06.721:E / AndroidRuntime(25709):在android.graphics.BitmapFactory.de codeResource(BitmapFactory.java:374)
九月六号至21日:32:06.721:E / AndroidRuntime(25709):在android.graphics.BitmapFactory.de codeResource(BitmapFactory.java:404)
九月六号至21日:32:06.721:E / AndroidRuntime(25709):在

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

解决方案

位图的最大尺寸为2048×2048,如果你的位图大于这个你应该扩大它..
我用这个方法来扩展位图:

 公共静态位图scaleBitmap(位图bitmapToScale,浮newWidth,浮newHeight){
    如果(bitmapToScale == NULL)
        返回null;
    //获取原始宽度和高度
    INT宽度= bitmapToScale.getWidth();
    INT高= bitmapToScale.getHeight();
    //创建矩阵用于操作
    字模=新的Matrix();

    //调整位图
    matrix.postScale(newWidth /宽,newHeight /高);

    //重新创建新的位图,并将其回
    返回Bitmap.createBitmap(bitmapToScale,0,0,bitmapToScale.getWidth(),bitmapToScale.getHeight(),矩阵,真);
}
 

您也可以使用此检查内存状态:

  ActivityManager actMgr =(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo明复=新ActivityManager.MemoryInfo();
actMgr.getMemoryInfo(明复);
如果(minfo.lowMemory){//做一些事情}
 

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 

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

解决方案

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}

这篇关于ImageView的OutOfMemoryException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆