Set CameraX image capture File path

≯℡__Kan透↙ 提交于 2021-01-28 19:56:12

问题


I set the File path to the following:

File file = new File(Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".png");

    ImageCapture.OutputFileOptions outputFileOptions =
            new ImageCapture.OutputFileOptions.Builder(file).build();

    imageCapture.takePicture(outputFileOptions, Executors.newSingleThreadExecutor(),
            new ImageCapture.OnImageSavedCallback() {
                @Override
                public void onImageSaved(ImageCapture.OutputFileResults outputFileResults) {
                    // insert your code here.
                    Log.d("PHOTO", "onImageSaved: saved");
                }
                @Override
                public void onError(ImageCaptureException error) {
                    // insert your code here.
                    Log.d("PHOTO", "onError: " + error);

                }
            });

But for some reason when i click on the capture image button, it jumps into the onError and logs the following:

onError: androidx.camera.core.ImageCaptureException: Failed to write or close the file


回答1:


Your app needs permission to write to the desired location.

You could request WRITE_EXTERNAL_STORAGE, both in the manifest and at runtime, to allow you to write where you requested... up through Android 9. Android 10 and beyond do not support ordinary apps creating arbitrary directories in the root of external storage.

Your choice of getExternalFilesDir() means that you do not need any permissions. The downside is that the file(s) that you put there get removed when your app is uninstalled.



来源:https://stackoverflow.com/questions/61856053/set-camerax-image-capture-file-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!