简体   繁体   English

设置 CameraX 图像捕获文件路径

[英]Set CameraX image capture File path

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 并记录以下内容:

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

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.您可以在清单和运行时请求WRITE_EXTERNAL_STORAGE ,以允许您在请求的位置写入...直到 Android 9。 Android 10 及以后不支持普通应用程序在外部存储的根目录中创建任意目录。

Your choice of getExternalFilesDir() means that you do not need any permissions.您选择getExternalFilesDir()意味着您不需要任何权限。 The downside is that the file(s) that you put there get removed when your app is uninstalled.缺点是当您的应用程序被卸载时,您放置在那里的文件会被删除。

according to the latest updates for android R (API level 30) and Android storage use cases and best practices , you might use the code below to save your image in your app's cache storage根据 android R(API 级别 30)和Android 存储用例和最佳实践的最新更新,您可以使用以下代码将图像保存在应用的缓存存储中

File file = new File(getContext().getExternalCacheDir() + File.separator + System.currentTimeMillis() + ".png");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM