Search code examples
androidout-of-memoryandroid-bitmapbitmapimagebitmapfactory

Error is java.lang.OutOfMemoryError


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

            }
        });

Issue is the App is working too slowly due to memoryError the error is when button is clicked on image it will be seen in fullscreen image and again clicked the image will be again original. this is error:

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

Thank you in advance


Solution

  • Use options with decodeFile method like this:

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