Android应用程序崩溃了一段时间,并且在没有崩溃的情况下运行良好

时间:2017-10-04 09:06:50

标签: android android-camera2

我正在使用Android Camera2 API并想连续拍摄15张照片。问题是它成功拍了几张照片但在6~8张照片后崩溃了。

以下是日志:

  

Legacy-CameraDevice-JNI:LegacyCameraDevice_nativeGetSurfaceId:可以   不从曲面检索本机Surface。 E / AndroidRuntime:致命   EXCEPTION:Thread-1095                                                                                    处理:com.example.grobomac.traindriver,PID:11690                                                                                     java.lang.IllegalArgumentException:Surface没有有效的本机   表面。                                                                                             在   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(RequestThreadManager.java:225)                                                                                             在   android.hardware.Camera $ EventHandler.handleMessage(Camera.java:1272)                                                                                             在android.os.Handler.dispatchMessage(Handler.java:111)                                                                                             在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.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();
                            }
                       }
                    }

我将在OnResume拍照:

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


            }

1 个答案:

答案 0 :(得分:0)

不确定这是否相关,但您在使用后不会回收位图。垃圾收集器(GC)应该从Android版本3及更高版本中为您带来这个,但是您正在快速创建新的,也许它没有时间。我没有注意到您的堆栈跟踪中的任何OOM异常,因此可能不是这样,但您应该回收,因此GC不需要做太多工作。