错误是java.lang.OutOfMemoryError

时间:2016-08-02 10:17:12

标签: android out-of-memory android-bitmap bitmapimage bitmapfactory

        File imgFile = new File(path);

        if (imgFile.exists()) {
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            options.inSampleSize = 2;
            options.inJustDecodeBounds = false;
            options.inTempStorage = new byte[16 * 1024];

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        //Drawable d = new BitmapDrawable(getResources(), myBitmap);
        float scale = context.getResources().getDisplayMetrics().density;
        holder.userprofile.setImageBitmap(myBitmap);
        holder.userprofile.setLayoutParams(new LinearLayout.LayoutParams((int) (350 * scale), (int) (300 * scale)));

        holder.userprofile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isImageFitToScreen) {
                    Toast.makeText(context, "NORMAL SIZE!", Toast.LENGTH_LONG).show();


                    float scale = context.getResources().getDisplayMetrics().density;
                    holder.userprofile.setLayoutParams(new LinearLayout.LayoutParams((int) (350 * scale), (int) (300 * scale)));
                    holder.userprofile.setAdjustViewBounds(true);


                    isImageFitToScreen = false;
                } else {
                    Toast.makeText(context, "FULLSCREEN!", Toast.LENGTH_LONG).show();
                    float scaleHeigth = context.getResources().getDisplayMetrics().heightPixels;
                    float scaleWidth = context.getResources().getDisplayMetrics().widthPixels;
                    holder.userprofile.setLayoutParams(new LinearLayout.LayoutParams((int) scaleWidth, (int) scaleHeigth));
                    holder.userprofile.setScaleType(ImageView.ScaleType.FIT_XY);


                    isImageFitToScreen = true;
                }

            }
        });

问题是由于memoryError,应用程序工作得太慢错误是当在图像上单击按钮时,它将在全屏图像中看到并再次单击该图像将再次原始。 这是错误:

java.lang.OutOfMemoryError: Failed to allocate a 63701004 byte allocation with 16777120 free bytes and 49MB until OOM

提前谢谢

1 个答案:

答案 0 :(得分:0)

options使用decodeFile方法,如下所示:

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);