java.lang.OutOfMemoryError处理来自Camera的14个数据

问题描述

我正在制作一个审计应用程序,必须从单选按钮和14个检查点的图像中收集是/否。

该应用程序在装有Android 10设备的设备上运行正常,但是在以某种随机方式收集4-6张图像并在相应的Imageview上显示它们并在String中收集Byte []后,在某些设备上(如在某些牛轧糖设备上)进行测试变量,应用程序也不会在日志中崩溃(现在通过调试器找到它,请参见下文)。看一下相机的代码。恢复活动后,单击照片后该应用程序崩溃了。


 public void ClickImageFromCamera(String code) {


        Intent CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        File destination = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis() + ".jpg");
        String img_Decodable_Str = String.valueOf(destination);
        Log.e("img fromCam",String.valueOf(destination));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(getActivity(),getActivity().getApplicationContext().getPackageName() + ".provider",destination);
            CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,uri);
        } else {
            uri = Uri.fromFile(destination);
            CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,uri);
        }
        CamIntent.putExtra("return-data",true);
        startActivityForResult(CamIntent,Integer.parseInt(code));






    }

 @Override
    public void onActivityResult(int requestCode,int resultCode,Intent data) {
        Log.e("Request code",String.valueOf(resultCode));
        Log.e("ResultCode ",String.valueOf(resultCode));
        if (requestCode == 50) {
            if (resultCode == -1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    //this is above marshmellow
                    previewCapturedImage(50);
                } else {
                    //this is below marshmellow
                    previewCapturedImage1(50);
                }


            } else if (resultCode == 0) {
                // user cancelled Image capture
                Toast.makeText(getActivity(),"User cancelled image capture",Toast.LENGTH_SHORT)
                        .show();
            }

        }else {
            // Failed to capture image
            // Toast.makeText(getActivity(),"Sorry! Failed to capture image",Toast.LENGTH_SHORT).show();
        }


        if (requestCode == 51) {
            if (resultCode == -1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    //this is above marshmellow
                    previewCapturedImage(51);
                } else {
                    //this is below marshmellow
                    previewCapturedImage1(51);
                }

            } else if (resultCode == 0) {
                // user cancelled Image capture
                Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
                        .show();
            } else {
                // Failed to capture image

            }

        }
        if (requestCode == 52) {
            if (resultCode == -1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    //this is above marshmellow
                    previewCapturedImage(52);
                } else {
                    //this is below marshmellow
                    previewCapturedImage1(52);
                }

            } else if (resultCode == 0) {
                // user cancelled Image capture
                Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
                        .show();
            } else {
                // Failed to capture image

            }

        }
        if (requestCode == 53) {
            if (resultCode == -1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    //this is above marshmellow
                    previewCapturedImage(53);
                } else {
                    //this is below marshmellow
                    previewCapturedImage1(53);
                }

            } else if (resultCode == 0) {
                // user cancelled Image capture
                Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
                        .show();
            } else {
                // Failed to capture image

            }

        }
        if (requestCode == 54) {
            if (resultCode == -1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    //this is above marshmellow
                    previewCapturedImage(54);
                } else {
                    //this is below marshmellow
                    previewCapturedImage1(54);
                }

            } else if (resultCode == 0) {
                // user cancelled Image capture
                Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
                        .show();
            } else {
                // Failed to capture image

            }

        }
        if (requestCode == 55) {
            if (resultCode == -1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    //this is above marshmellow
                    previewCapturedImage(55);
                } else {
                    //this is below marshmellow
                    previewCapturedImage1(55);
                }

            } else if (resultCode == 0) {
                // user cancelled Image capture
                Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
                        .show();
            } else {
                // Failed to capture image

            }

        }
        if (requestCode == 56) {
            if (resultCode == -1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    //this is above marshmellow
                    previewCapturedImage(56);
                } else {
                    //this is below marshmellow
                    previewCapturedImage1(56);
                }

            } else if (resultCode == 0) {
                // user cancelled Image capture
                Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
                        .show();
            } else {
                // Failed to capture image

            }

        }
        if (requestCode == 57) {
            if (resultCode == -1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    //this is above marshmellow
                    previewCapturedImage(57);
                } else {
                    //this is below marshmellow
                    previewCapturedImage1(57);
                }

            } else if (resultCode == 0) {
                // user cancelled Image capture
                Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
                        .show();
            } else {
                // Failed to capture image

            }

        }
        if (requestCode == 58) {
            if (resultCode == -1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    //this is above marshmellow
                    previewCapturedImage(58);
                } else {
                    //this is below marshmellow
                    previewCapturedImage1(58);
                }

            } else if (resultCode == 0) {
                // user cancelled Image capture
                Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
                        .show();
            } else {
                // Failed to capture image

            }

        }
        if (requestCode == 59) {
            if (resultCode == -1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    //this is above marshmellow
                    previewCapturedImage(59);
                } else {
                    //this is below marshmellow
                    previewCapturedImage1(59);
                }

            } else if (resultCode == 0) {
                // user cancelled Image capture
                Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
                        .show();
            } else {
                // Failed to capture image

            }

        }
        if (requestCode == 60) {
            if (resultCode == -1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    //this is above marshmellow
                    previewCapturedImage(60);
                } else {
                    //this is below marshmellow
                    previewCapturedImage1(60);
                }

            } else if (resultCode == 0) {
                // user cancelled Image capture
                Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
                        .show();
            } else {
                // Failed to capture image

            }

        }
        if (requestCode == 61) {
            if (resultCode == -1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    //this is above marshmellow
                    previewCapturedImage(61);
                } else {
                    //this is below marshmellow
                    previewCapturedImage1(61);
                }

            } else if (resultCode == 0) {
                // user cancelled Image capture
                Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
                        .show();
            } else {
                // Failed to capture image

            }

        }
        if (requestCode == 62) {
            if (resultCode == -1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    //this is above marshmellow
                    previewCapturedImage(62);
                } else {
                    //this is below marshmellow
                    previewCapturedImage1(62);
                }

            } else if (resultCode == 0) {
                // user cancelled Image capture
                Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
                        .show();
            } else {
                // Failed to capture image

            }

        }
        if (requestCode == 63) {
            if (resultCode == -1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    //this is above marshmellow
                    previewCapturedImage(63);
                } else {
                    //this is below marshmellow
                    previewCapturedImage1(63);
                }

            } else if (resultCode == 0) {
                // user cancelled Image capture
                Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
                        .show();
            } else {
                // Failed to capture image

            }

        }


    }

 public void previewCapturedImage(int code) {

        try {



            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;
            // ContentResolver contentResolver = getContentResolver();
            InputStream inputStream = null;

            try {
                inputStream = getActivity().getContentResolver().openInputStream(uri);
            } catch (FileNotFoundException e) {
                e.printstacktrace();
            }


            // image_stream = mContext.getContentResolver().openInputStream(sendUri);
            bitmap = BitmapFactory.decodeStream(inputStream);
            bitmap.compress(Bitmap.CompressFormat.JPEG,50,byteArrayOutputStream);
            byte[] imageBytes = byteArrayOutputStream.toByteArray();


            int nh = (int) (bitmap.getHeight() * (512.0 / bitmap.getWidth()));
            Bitmap scaled = Bitmap.createScaledBitmap(bitmap,512,nh,true);
            scaled.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
           /* byte[] imageBytes1 = byteArrayOutputStream.toByteArray();
            String encoded = Base64.encodetoString(imageBytes1,Base64.NO_WRAP);
            Log.e("encode",encoded);*/
            Log.e("Original   dimensions",bitmap.getWidth() + " " + bitmap.getHeight());
            Log.e("Compressed dimensions",scaled.getWidth() + " " + scaled.getHeight());
            /*int size = scaled.getRowBytes() * scaled.getHeight();
            Log.e("size",String.valueOf(size));
            java.nio.ByteBuffer b = java.nio.ByteBuffer.allocate(size);

            scaled.copyPixelsToBuffer(b);
 byte[] bytes = new byte[size];
            Log.e("bytes",String.valueOf(bytes));
*/

            if(code ==50) {
                imageCode1 = Base64.encodetoString(imageBytes,Base64.NO_WRAP);
                Log.e("image1",imageCode1);
                set_img_fascia.setimageBitmap(bitmap);

            }
            if(code ==51) {
                imageCode2 = Base64.encodetoString(imageBytes,imageCode2);
                set_img_lollypop.setimageBitmap(bitmap);

            }
            if(code ==52) {
                imageCode3 = Base64.encodetoString(imageBytes,imageCode3);
                set_img_ATM_Count.setimageBitmap(bitmap);

            }
            if(code ==53) {
                imageCode4 = Base64.encodetoString(imageBytes,imageCode4);
                set_img_ATM_Cooling.setimageBitmap(bitmap);

            }
            if(code ==54) {
                imageCode5 = Base64.encodetoString(imageBytes,imageCode5);
                set_img_ATM_Backroom_Locked.setimageBitmap(bitmap);

            }
            if(code ==55) {
                imageCode6 = Base64.encodetoString(imageBytes,imageCode6);
                set_img_ups_room.setimageBitmap(bitmap);

            }
            if(code ==56) {
                imageCode7 = Base64.encodetoString(imageBytes,imageCode7);
                set_img_Branch_Shutter_damaged.setimageBitmap(bitmap);

            }
            if(code ==57) {
                imageCode8 = Base64.encodetoString(imageBytes,imageCode8);
                set_img_Branch_Round_Taken.setimageBitmap(bitmap);

            }
            if(code ==58) {
                imageCode9 = Base64.encodetoString(imageBytes,imageCode9);
                set_img_AC_ODU_copper_Intact.setimageBitmap(bitmap);

            }
            if(code ==59) {
                imageCode10 = Base64.encodetoString(imageBytes,imageCode10);
                set_img_Any_Water_Logging_Vincinity.setimageBitmap(bitmap);

            }
            if(code ==60) {
                imageCode11 = Base64.encodetoString(imageBytes,imageCode11);
                set_img_FE_Room_Available.setimageBitmap(bitmap);

            }
            if(code ==61) {
                imageCode12 = Base64.encodetoString(imageBytes,imageCode12);
                set_img_DG_Door_Locked.setimageBitmap(bitmap);

            }
            if(code ==62) {
                imageCode13 = Base64.encodetoString(imageBytes,imageCode13);
                set_img_Other_Obsevation.setimageBitmap(bitmap);

            }
            if(code ==63) {
                imageCode14 = Base64.encodetoString(imageBytes,imageCode14);
                set_img_damage_ATM_lobby.setimageBitmap(bitmap);

            }
/*
//decode base64 string to image
            imageBytes = Base64.decode(imageCode,Base64.DEFAULT);
            Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes,imageBytes.length);
            set_img_trainy.setimageBitmap(decodedImage);*/

        } catch (NullPointerException e) {
            e.printstacktrace();
        }

    }


    public void previewCapturedImage1(int code) {

        try {
            //encode base64 string to image
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            // Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.img);
            // bimatp factory
            BitmapFactory.Options options = new BitmapFactory.Options();

            // downsizing image as it throws OutOfMemory Exception for larger
            options.inSampleSize = 8;

            bitmap = BitmapFactory.decodeFile(uri.getPath(),options);

            bitmap.compress(Bitmap.CompressFormat.JPEG,byteArrayOutputStream);
            byte[] imageBytes = byteArrayOutputStream.toByteArray();
            // byte[] imageBytes = byteArrayOutputStream.toByteArray();
            //imageCode = Base64.encodetoString(imageBytes,Base64.DEFAULT);
            // Log.e("image_code",imageCode);

            if(code ==50) {
                imageCode1 = Base64.encodetoString(imageBytes,imageCode14);
                set_img_damage_ATM_lobby.setimageBitmap(bitmap);

            }


        } catch (NullPointerException e) {
            e.printstacktrace();
        }

    }


 public void previewCapturedImage1(int code) {

        try {
            //encode base64 string to image
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            // Bitmap bitmap = BitmapFactory.decodeResource(getResources(),imageCode14);
                set_img_damage_ATM_lobby.setimageBitmap(bitmap);

            }


        } catch (NullPointerException e) {
            e.printstacktrace();
        }

    }

在调试器中发现错误

FATAL EXCEPTION: main
    Process: com.field_officer,PID: 15547
    java.lang.OutOfMemoryError: Failed to allocate a 14745612 byte allocation with 4194304 free bytes and 8MB until OOM
        at dalvik.system.vmruntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
        at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:681)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:657)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:701)
        at com.field_officer.Fragment.ICICI_report_Static_Fragment.previewCapturedImage(ICICI_report_Static_Fragment.java:794)
        at com.field_officer.Fragment.ICICI_report_Static_Fragment.onActivityResult(ICICI_report_Static_Fragment.java:1506)
        at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:156)
        at com.field_officer.HomeActivity.onActivityResult(HomeActivity.java:740)
        at android.app.Activity.dispatchActivityResult(Activity.java:6950)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4176)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4223)
        at android.app.ActivityThread.-wrap20(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1585)
        at android.os.Handler.dispatchMessage(Handler.java:110)

解决方法

好,我解决了。


            int nh = (int) (bitmap.getHeight() * (512.0 / bitmap.getWidth()));
            Bitmap scaled = Bitmap.createScaledBitmap(bitmap,512,nh,true);
            scaled.compress(Bitmap.CompressFormat.JPEG,80,byteArrayOutputStream);
            byte[] imageBytes = byteArrayOutputStream.toByteArray();

         
            Log.e("Original   dimensions",bitmap.getWidth() + " " + bitmap.getHeight());
            Log.e("Compressed dimensions",scaled.getWidth() + " " + scaled.getHeight());
              Log.e("bytes",String.valueOf(bytes));

          bitmap =null;
            if(code ==50) {
                imageCode1 = Base64.encodeToString(imageBytes,Base64.NO_WRAP);
                Log.e("image1",imageCode1);
                set_img_fascia.setImageBitmap(scaled);

            }
            if(code ==51) {
                imageCode2 = Base64.encodeToString(imageBytes,imageCode2);
                set_img_lollypop.setImageBitmap(scaled);

...So on