加载大位图后,仍然得到java.lang.OutOfMemoryError高效训练

时间:2016-05-26 15:29:56

标签: java android bitmap

我跟踪了[this] Android培训,了解如何在不耗尽已分配内存的情况下显示大型位图。我按照教程,我的代码没有错误。但是,当我打开应用程序进入应该加载大图像的活动时,我仍然会收到一条错误消息,说我的内存不足。

这是我的代码:

public class Map extends AppCompatActivity {

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

        int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
            while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }

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

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

    public void touchMap() {
        ImageView mapOverlay = (ImageView) findViewById(R.id.mapOverlay);

        final Bitmap bitmap = ((BitmapDrawable) mapOverlay.getDrawable()).getBitmap();
        mapOverlay.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int) event.getX();
                int y = (int) event.getY();
                int pixel = bitmap.getPixel(x, y);

                int redVal = Color.red(pixel);
                int bluVal = Color.blue(pixel);
                int greVal = Color.green(pixel);
                int alpVal = Color.alpha(pixel);

                Log.i("ColorTouch", redVal + ", " + bluVal + ", " + greVal + ", " + alpVal);

                if (alpVal == 0) {
                    ImageView doNothing = (ImageView) findViewById(R.id.mapOverlay);

                    doNothing.setOnClickListener(null);
                } else if (redVal == 255 && bluVal == 0 && greVal == 0 && alpVal == 255) {
                    ImageView openBed1 = (ImageView) findViewById(R.id.mapOverlay);

                    openBed1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent toBed1 = new Intent(Map.this, Bed1.class);
                            startActivity(toBed1);
                        }
                    });
                } else if (redVal == 255 && bluVal == 255 && greVal == 0 && alpVal == 255) {
                    ImageView openBed2 = (ImageView) findViewById(R.id.mapOverlay);

                    openBed2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent toBed2 = new Intent(Map.this, Bed2.class);
                            startActivity(toBed2);
                        }
                    });
                } else if (redVal == 0 && bluVal == 255 && greVal == 0 && alpVal == 255) {
                    ImageView openBed3 = (ImageView) findViewById(R.id.mapOverlay);

                    openBed3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent toBed3 = new Intent(Map.this, Bed3.class);
                            startActivity(toBed3);
                        }
                    });
                } else if (redVal == 0 && greVal == 255 && bluVal == 255 && alpVal == 255) {
                    ImageView openBed4 = (ImageView) findViewById(R.id.mapOverlay);

                    openBed4.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent toBed4 = new Intent(Map.this, Bed4.class);
                            startActivity(toBed4);
                        }
                    });
                } else if (redVal == 0 && greVal == 255 && bluVal == 0 && alpVal == 255) {
                    ImageView openBed5 = (ImageView) findViewById(R.id.mapOverlay);

                    openBed5.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent toBed5 = new Intent(Map.this, Bed5.class);
                            startActivity(toBed5);
                        }
                    });
                } else if (redVal == 255 && greVal == 255 && bluVal == 0 && alpVal == 255) {
                    ImageView openBed6 = (ImageView) findViewById(R.id.mapOverlay);

                    openBed6.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent toBed6 = new Intent(Map.this, Bed6.class);
                            startActivity(toBed6);
                        }
                    });
                } else if (redVal == 0 && greVal == 0 && bluVal == 0 && alpVal == 255) {
                    ImageView openBed7 = (ImageView) findViewById(R.id.mapOverlay);

                    openBed7.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent toBed7 = new Intent(Map.this, Bed7.class);
                            startActivity(toBed7);
                        }
                    });
                } else if (redVal == 255 && greVal == 255 && bluVal == 255 && alpVal == 255) {
                    ImageView openBed8 = (ImageView) findViewById(R.id.mapOverlay);

                    openBed8.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent toBed8 = new Intent(Map.this, Bed8.class);
                            startActivity(toBed8);
                        }
                    });
                } else if (redVal == 96 && greVal == 0 && bluVal == 255 && alpVal == 255) {
                    ImageView openBed9 = (ImageView) findViewById(R.id.mapOverlay);

                    openBed9.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent toBed9 = new Intent(Map.this, Bed8.class);
                            startActivity(toBed9);
                        }
                    });
                }
                return false;
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        touchMap();

        Typeface serif = Typeface.createFromAsset(getAssets(), "fonts/Playfair.otf");
        ((Button) findViewById(R.id.buttonMapToHome)).setTypeface(serif);

        Button goHome = (Button) findViewById(R.id.buttonMapToHome);
        goHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent toHome = new Intent(Map.this, Home.class);
                startActivity(toHome);
            }
        });

        HorizontalScrollView horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView);
        horizontalScrollView.setHorizontalScrollBarEnabled(false);

        ImageView mImageView = new ImageView(this);
        mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.id.map, 100, 100));
    }
}

这是我的错误消息:

java.lang.OutOfMemoryError: Failed to allocate a 61054012 byte allocation with 16777216 free bytes and 26MB until OOM
                            at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                            at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
                            at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
                            at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
                            at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
                            at android.content.res.Resources.loadDrawable(Resources.java:2540)
                            at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
                            at android.widget.ImageView.<init>(ImageView.java:152)
                            at android.widget.ImageView.<init>(ImageView.java:140)
                            at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:58)
                            at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:54)
                            at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:95)
                            at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:938)
                            at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:992)
                            at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
                            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
                            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
                            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                            at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                            at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
                            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
                            at org.havenseedsforsuccess.havenseedsforsuccess.Map.onCreate(Map.java:171)
                            at android.app.Activity.performCreate(Activity.java:6251)
                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                            at android.app.ActivityThread.-wrap11(ActivityThread.java)
                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                            at android.os.Handler.dispatchMessage(Handler.java:102)
                            at android.os.Looper.loop(Looper.java:148)
                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                            at java.lang.reflect.Method.invoke(Native Method)
                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

0 个答案:

没有答案