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

如何解决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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-