Android OutOfMemoryError:?

前端 未结 8 1497
孤街浪徒
孤街浪徒 2020-11-28 06:02

I am sporadically getting an OutOfMemoryError: (Heap Size=49187KB, Allocated=41957KB) in one of my apps. What can I do to diagnose this?

  01-09         


        
8条回答
  •  無奈伤痛
    2020-11-28 06:51

    final Bitmap smile = BitmapFactory.decodeResource(getResources(), R.drawable.emo_im_happy);

    Call

    String pathname=BitMapToString(smile);

    and then call

    setImagesNew(linearview,pathname,activity);
    

    ...

    public String BitMapToString(Bitmap bitmap) { 
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
                 bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
            byte[] b = baos.toByteArray();
          String temp = Base64.encodeToString(b, Base64.DEFAULT);
          return temp;
     }
    
    
     public static void setImagesNew(LinearLayout linearLayout, String pathName,
                    Activity activity) {
    
                Bitmap bmp = decodeSampledBitmapFromResource(pathName,
                        getDeviceWidth(activity), getDeviceHeight(activity));
    
    linearLayout.setBackgroundDrawable(bmp);
    
    
                bmp = null;
                System.gc();
                Runtime.getRuntime().gc();
    
            }
        public static Bitmap decodeSampledBitmapFromResource(String pathName,
                int reqWidth, int reqHeight) {
    
            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(pathName, options);
    
            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth,
                    reqHeight);
    
            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(pathName, options);
        }
    
        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) {
    
                final int halfHeight = height / 2;
                final int halfWidth = width / 2;
    
                // Calculate the largest inSampleSize value that is a power of 2 and
                // keeps both
                // height and width larger than the requested height and width.
                while ((halfHeight / inSampleSize) > reqHeight
                        && (halfWidth / inSampleSize) > reqWidth) {
                    inSampleSize *= 2;
                }
            }
    
            return inSampleSize;
        }
    
        @SuppressLint("NewApi")
        public static int getDeviceWidth(Activity activity) {
            int deviceWidth = 0;
    
            Point size = new Point();
            WindowManager windowManager = activity.getWindowManager();
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                windowManager.getDefaultDisplay().getSize(size);
                deviceWidth = size.x;
            } else {
                Display display = windowManager.getDefaultDisplay();
                deviceWidth = display.getWidth();
            }
            return deviceWidth;
        }
    
        @SuppressLint("NewApi")
        public static int getDeviceHeight(Activity activity) {
            int deviceHeight = 0;
    
            Point size = new Point();
            WindowManager windowManager = activity.getWindowManager();
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                windowManager.getDefaultDisplay().getSize(size);
                deviceHeight = size.y;
            } else {
                Display display = windowManager.getDefaultDisplay();
                deviceHeight = display.getHeight();
            }
            return deviceHeight;
        }
    

    please put all function in your activity and call only setImageNew() and pass parameter in imageview ,sdcardpathname and activity

    I hope it will not crash after you implement this code. because I arise same problem as you..

提交回复
热议问题