线程退出与未捕获的异常android(OutOfMemory)

时间:2014-07-07 08:18:00

标签: android eclipse multithreading bitmap out-of-memory

它有时有效但有时不起作用 任何人都可以帮助我找出问题并告诉我如何应对它们。谢谢!

错误日志:

07-07 04:07:48.583: W/dalvikvm(3030): threadid=1: thread exiting with uncaught exception (group=0xb3afeba8)
07-07 04:07:48.603: E/AndroidRuntime(3030): FATAL EXCEPTION: main
07-07 04:07:48.603: E/AndroidRuntime(3030): Process: com.example.kingdoms, PID: 3030
07-07 04:07:48.603: E/AndroidRuntime(3030): java.lang.OutOfMemoryError
07-07 04:07:48.603: E/AndroidRuntime(3030):     at android.graphics.Bitmap.nativeCreate(Native Method)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at android.graphics.Bitmap.createBitmap(Bitmap.java:786)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at android.graphics.Bitmap.createBitmap(Bitmap.java:718)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:594)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at com.example.kingdoms.war1.onCreate(war1.java:1433)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at android.app.Activity.performCreate(Activity.java:5231)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at android.os.Looper.loop(Looper.java:136)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at java.lang.reflect.Method.invokeNative(Native Method)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at java.lang.reflect.Method.invoke(Method.java:515)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-07 04:07:48.603: E/AndroidRuntime(3030):     at dalvik.system.NativeStart.main(Native Method)
07-07 04:12:48.683: I/Process(3030): Sending signal. PID: 3030 SIG: 9

1 个答案:

答案 0 :(得分:2)

首先,必须防止OOM异常无法处理。

OOM通常导致bitmaps异常导致您的应用程序崩溃,导致应用程序崩溃。为了避免OOM您可以缩小bitmaps如果它们太大,则在将它们加载到内存之前。您可以使用BitmapFactory.Options完成此操作,并根据您的要求进行缩减。

    public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
                int reqHeight) {

       final BitmapFactory.Options options = new BitmapFactory.Options();
       options.inJustDecodeBounds = true;
       BitmapFactory.decodeFile(path, options);

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

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap bmp = BitmapFactory.decodeFile(path, options);
        return bmp;
    }

    public static int calculateInSampleSize(BitmapFactory.Options options,
             int reqWidth, int reqHeight) {

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