Android Studio 当应用程序转到后台时,摄像头无法拍摄图像,但当应用程序转到前台时,摄像头工作正常

cl25kdpy  于 2023-01-02  发布在  Android
关注(0)|答案(1)|浏览(196)

我正在制作的应用程序,从firebase远程命令前置摄像头的图像。应用程序工作正常,无需用户交互就可以拍照,但当应用程序关闭或转到前台时,应用程序开始给出错误,无法连接到相机服务。只要应用程序打开它就可以捕捉图像。
我运行前台服务通知,其中工作,但仍然相同的相机失败错误,无法拍照。

try {

                    Log.d("kkkk", "Preparing to take photo");

                    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();

                    int frontCamera = cam;
                    //int backCamera=0;

                    Camera.getCameraInfo(frontCamera, cameraInfo);

                    try {
                        camera = Camera.open(frontCamera);
                    } catch (RuntimeException e) {
                        Log.d("kkkk", "Camera not available: " + e.getMessage());
                        camera = null;
//                        takePicture(0);
                    }
                    try {
                        if (null == camera) {
                            Log.d("kkkk", "Could not get camera instance");
                        } else {
                            Log.d("kkkk", "Got the camera, creating the dummy surface texture");
                            try {
                                camera.setPreviewTexture(new SurfaceTexture(0));
                                camera.startPreview();
                            } catch (Exception e) {
                                Log.d("kkkk", "Could not set the surface preview texture");
                                e.printStackTrace();
                            }
                            camera.takePicture(null, null, new Camera.PictureCallback() {

                                @Override
                                public void onPictureTaken(byte[] data, Camera camera) {

                                    Log.d("kkkk", "clicked");


                                    // Encode the byte array into a base64 string
//                                    String imageString = android.util.Base64.encodeToString(imageBytes, android.util.Base64.DEFAULT);
//                                    Log.d("error200", imageString);

                                    FirebaseStorage storage = FirebaseStorage.getInstance();

                                    StorageReference storageRef = storage.getReference();

                                    String path = "images/"+username.toLowerCase()+device.replace(" ","");

                                    StorageReference imageRef = storageRef.child(path);

                                    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); // Replace this with your bitmap image
                                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                    bitmap.compress(Bitmap.CompressFormat.JPEG, 10, baos);
                                    byte[] data0 = baos.toByteArray();

                                    UploadTask uploadTask = imageRef.putBytes(data0);
                                    uploadTask.addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception exception) {
                                            // Handle unsuccessful uploads
                                            Log.d("pic","fail"+exception.getMessage());
                                        }
                                    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                        @Override
                                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                            // Handle successful uploads
                                            Log.d("pic","done");
                                        }
                                    });

                                    camera.release();

                                }
                            });
                        }
                    } catch (Exception e) {
                        camera.release();
                    }

                } catch (Exception e) {
                    Log.d("errorData", e.getMessage());
                }

onDestroy方法我释放了摄像机,但仍然出现相同的错误。

@Override
    public void onDestroy() {
        super.onDestroy();

        if (camera != null) {
            camera.stopPreview();
            camera.release();
            camera = null;
        }```
siv3szwd

siv3szwd1#

你能在清单中添加START_ACTIVITIES_FROM_BACKGROUND权限吗?如果你还没有添加,那么你需要在前台服务中添加使用相机的权限。但是我不确定安卓新版本是否支持在没有用户交互的情况下使用相机
请参阅此处的官方Android文档

相关问题