即使大位图被有效处理,OutOfMemory也会出错

时间:2014-09-01 05:14:24

标签: android nullpointerexception out-of-memory android-imageview android-bitmap

我正在开发导游应用程序。我正在使用鳍状肢视图来拍摄图像。我已经解码了位图。但它仍然给出了outofmemoryError。请帮我解决一下这个。提前致谢

  

09-01 10:30:47.595:E / AndroidRuntime(30992):致命异常:主

     

09-01 10:30:47.595:E / AndroidRuntime(30992):   java.lang.RuntimeException:无法启动活动   ComponentInfo {com.isuru.allaboutkataragama / com.isuru.allaboutkataragama.Thissa}:   android.view.InflateException:二进制XML文件行#63:错误   膨胀班

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.app.ActivityThread.access $ 700(ActivityThread.java:159)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1316)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   dalvik.system.NativeStart.main(Native Method)09-01 10:30:47.595:   E / AndroidRuntime(30992):引起:android.view.InflateException:   二进制XML文件行#63:错误输出类

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.view.LayoutInflater.createView(LayoutInflater.java:626)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.view.LayoutInflater.rInflate(LayoutInflater.java:761)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.view.LayoutInflater.rInflate(LayoutInflater.java:769)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.view.LayoutInflater.rInflate(LayoutInflater.java:769)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.view.LayoutInflater.rInflate(LayoutInflater.java:769)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.view.LayoutInflater.rInflate(LayoutInflater.java:769)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.view.LayoutInflater.inflate(LayoutInflater.java:498)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.view.LayoutInflater.inflate(LayoutInflater.java:398)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.view.LayoutInflater.inflate(LayoutInflater.java:354)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.app.Activity.setContentView(Activity.java:1956)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   com.isuru.allaboutkataragama.Thissa.onCreate(Thissa.java:83)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):引起:   java.lang.reflect.InvocationTargetException

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   java.lang.reflect.Constructor.constructNative(Native Method)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   java.lang.reflect.Constructor.newInstance(Constructor.java:417)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.view.LayoutInflater.createView(LayoutInflater.java:600)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):引起:   java.lang.OutOfMemoryError

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.content.res.Resources.loadDrawable(Resources.java:2988)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.content.res.TypedArray.getDrawable(TypedArray.java:602)

     

09-01 10:30:47.595:E / AndroidRuntime(30992):at   android.widget.ImageView。(ImageView.java:131)09-01   10:30:47.595:E / AndroidRuntime(30992):at   android.widget.ImageView。(ImageView.java:121)

这是我的代码。

iv1=(ImageView) findViewById(R.id.imgvw_thissa1);
        iv2=(ImageView) findViewById(R.id.imgvw_thissa2);
        iv3=(ImageView) findViewById(R.id.imgvw_thissa3);
        iv4=(ImageView) findViewById(R.id.imgvw_thissa4);
        iv5=(ImageView) findViewById(R.id.imgvw_thissa_lake);

        iv1.setImageBitmap(
                decodeSampledBitmapFromResource(getResources(), R.drawable.tissamaharama, 400, 400));

        iv2.setImageBitmap(
                decodeSampledBitmapFromResource(getResources(), R.drawable.thissa2, 275, 183));

        iv3.setImageBitmap(
                decodeSampledBitmapFromResource(getResources(), R.drawable.thissa3, 400, 400));

        iv4.setImageBitmap(
                decodeSampledBitmapFromResource(getResources(), R.drawable.thissa7, 400, 400));

        iv5.setImageBitmap(
                decodeSampledBitmapFromResource(getResources(), R.drawable.thissalake, 400, 400));



    public static int calculateInSampleSize(  
            BitmapFactory.Options options, int reqWidth, int reqHeight) {  
    // Raw height and width of image  
    final int height = options.outHeight;  
    final int width = options.outWidth;  
    int inSampleSize = 1;  

    if (height > reqHeight || width > reqWidth) {  
        if (width > height) {  
            inSampleSize = Math.round((float)height / (float)reqHeight);  
        } else {  
            inSampleSize = Math.round((float)width / (float)reqWidth);  
        }  
    }  
    return inSampleSize;  
}  


    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

1 个答案:

答案 0 :(得分:0)

当你在java中内存不足时,首先必须确保堆大小。您必须扩展VM的内存使用量:

java -Xmx2048m -Xms256m

这意味着您的应用程序将以256 MB存储开始,然后最多可以升至2048。您必须使用2的倍数来定义内存值。

您还可以使用不同的单位(千字节,兆字节和千兆字节)来定义内存:

-Xmx1024k
-Xmx512m
-Xmx1g

在Android中,您可以在以下链接中执行此操作:

Dalvik Memory Allocation - How to change the default limits?