Android应用程序崩溃了一些时间,并且在没有崩溃的情况下工作了一段时间



我正在使用Android Camera2 API,并希望连续拍摄15张照片。问题在于,它成功拍摄了几张照片,但在6〜8张照片后坠毁。

这是日志:

旧版 - 夏列维斯 - jni:legacycameradevice_nativegetsurfaceid:可以 不能从表面检索天然表面。E/AndroidRuntime:致命 例外:线程1095 过程:com.example.grobomac.traindriver,PID:11690 java.lang.illegalargumentException:表面没有有效的本地 表面。 在 android.hardware.camera2.legacy.legacycameradevice.nativegetsurfaceid(本机 方法) 在 android.hardware.camera2.legacy.legacycameradevice.getsurfaceid(legacycameradevice.java:658) 在 android.hardware.camera2.legacy.legacycameradevice.containssurfaceid(legacycameradevice.java:678) 在 android.hardware.camera2.legacy.RequestThreadManager $ 2.OnpictureTaken(requessThreadManager.java:225) 在 android.hardware.camera $ eventhandler.handlemessage(camera.java:1272) at android.os.handler.dispatchmessage(Handler.java:111) at android.os.looper.loop(looper.java:207) 在 android.hardware.camera2.legacy.cameradeviceusershim $ cameralooper.run(cameradeviceusershim.java:136) 在java.lang.thread.run(thread.java:818) 10-04 14:26:59.566 11690-11690/com.example.grobomac.grobomac.traindriver e/androidcameraapi:onpause

和我使用的代码:

public void onImageAvailable(ImageReader reader) {
                    Image image = null;
                    try {
                        image = reader.acquireLatestImage();
                        ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                        byte[] bytes = new byte[buffer.capacity()];
                        buffer.get(bytes);
                        save(bytes);
                        mBitmapToSave1 = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                        mBitmapToSave = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                        Bitmap scaled = Bitmap.createScaledBitmap(mBitmapToSave, width, height, true);
                        int w = scaled.getWidth();
                        int h = scaled.getHeight();
                        // Setting post rotate to 90
                        Matrix mtx = new Matrix();
                        mtx.postRotate(-180);
                        // Rotating Bitmap
                        mBitmapToSave = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx, true);
                        // mBitmapToSave = Bitmap.createBitmap(width+rowPadding/pixelStride,height, Bitmap.Config.RGB_565);
                        // mBitmapToSave.copyPixelsToBuffer(buffer);
                        if (detector.isOperational() && mBitmapToSave != null) {
                            Frame frame = new Frame.Builder().setBitmap(mBitmapToSave).build();
                            SparseArray<Face> faces = detector.detect(frame);
                            for (index = 0; index < faces.size(); ++index) {
                                Face face = faces.valueAt(index);
                            }
                            if (faces.size() == 0) {
                                MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.not);
                                mediaPlayer.start();
                                //Toast.makeText(AndroidCamera2API.this, "Face Not detected Adjust Camera Properly", Toast.LENGTH_SHORT).show();
                            } else {
                                    c++;
                                    Toast.makeText(AndroidCamera2API.this, "Face Found " + "n", Toast.LENGTH_SHORT).show();
                                    //Toast.makeText(AndroidCamera2API.this, "Saved:" + file, Toast.LENGTH_SHORT).show();
                                    setFileToUpload();
                                    //file.delete();
                                   // if(file.exists()){
                                    //file.getCanonicalFile().delete();
                                   // if(file.exists()){
                                   //     getApplicationContext().deleteFile(file.getName());
                                   // }
                               // }
                                a++;
                                    Toast.makeText(AndroidCamera2API.this, "" +c, Toast.LENGTH_SHORT).show();

                               // Toast.makeText(AndroidCamera2API.this, "completed" , Toast.LENGTH_SHORT).show();

                            }
                        }
                        }catch(FileNotFoundException e){
                            e.printStackTrace();
                        } catch(IOException e){
                            e.printStackTrace();
                        } finally{
                            if (image != null) {
                                image.close();
                            }
                       }
                    }

我会在on Resume中拍照:

protected void onResume() {
        final Intent intent = new Intent(AndroidCamera2API.this, Completed.class);
        super.onResume();
        Log.e(TAG, "onResume");
            startBackgroundThread();
            if (textureView.isAvailable()) {
                openCamera();
            } else {
                textureView.setSurfaceTextureListener(textureListener);
            }
            final int PICTURES_LIMIT = 15;
            final Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    int pictureNo=0;
                    public void run() {
                        if (pictureNo>PICTURES_LIMIT) {
                            timer.cancel();
                            startActivity(intent);

                        }else {
                            pictureNo++;
                            takePicture();

                        }
                    }

                }, 10, 7500);

            }

不确定这是否相关,但是在使用后您不会回收位图。垃圾收集器(GC)应该从Android版本3及更高版本中为您带来此操作,但是您正在如此快速制作新的版本,也许没有时间。我没有注意到您的堆栈跟踪中的任何OOM例外情况,所以可能不是这样,但是无论如何您都应该回收,因此GC不必做很多工作。

最新更新