OnImageSavedCallback 返回的 OutputFileResults 具有无效的 Uri [英] OutputFileResults returned by OnImageSavedCallback has an invalid Uri

查看:39
本文介绍了OnImageSavedCallback 返回的 OutputFileResults 具有无效的 Uri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 CameraX 在我的 android 应用程序中拍照,保存它们,然后从它们的路径显示它们.在之前的 alpha-09 版本中,我可以使用 onImageSaved(File file) 来做到这一点.但是,对于 alpha-10,我必须使用 onImageSaved(OutputFileResults outputFileResults),然后从 outputFileResults 检索的 uri 中获取路径.但是我得到的 Uri 总是错误的.例如,当我的图像保存在:/external/images/media/1581680878237.jpg"时,我得到 uri 的路径:/external/images/media/113758".

I am using CameraX to take pictures in my android app, save them and then display them from their path. With the previous version alpha-09 I was able to do so with onImageSaved(File file). However with the alpha-10 I have to use onImageSaved(OutputFileResults outputFileResults) and then get the path from the uri retrieved by the outputFileResults. But the Uri I get is always wrong. For instance when my image is saved at: "/external/images/media/1581680878237.jpg" I get the uri's path: "/external/images/media/113758".

这是我的代码:

                ContentValues contentValues = new ContentValues();
                contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, "NEW_IMAGE");
                contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");

                ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions.Builder(
                        activity.getContentResolver(),
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        contentValues).build();

                imageCapture.takePicture(outputFileOptions, Runnable::run, new ImageCapture.OnImageSavedCallback() {
                    @Override
                    public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
                        Uri uri = outputFileResults.getSavedUri();
                        if(uri != null){
                            System.out.println("URI PATH" + uri.getPath());
                            System.out.println("URI PATH" + uri.toString());
                            activity.runOnUiThread(cameraProvider::unbindAll);
                            galleryAddPic(uri);
                            Bundle params = new Bundle();
                            params.putString("FILE_PATH", uri.getPath());
                            Navigation.findNavController(root).navigate(R.id.navigation_edit_image, params);
                        }
                    }

                    @Override
                    public void onError(@NonNull ImageCaptureException exception) {
                        exception.printStackTrace();
                    }
                });

推荐答案

所以我终于设法通过使用其他方法(尤其是其他 ImageCapture.OutputFileOptions.Builde)保存了 ImageCapture 拍摄的图像.我没有使用 Uri 对象来保存图像,而是使用了 File 对象.

So I finally managed to save the image taken by ImageCapture by using an other method (especially an other ImageCapture.OutputFileOptions.Builde). I didn't use an Uri object to save the image but a File object.

                File mImageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "YOUR_DIRECTORY");
                boolean isDirectoryCreated = mImageDir.exists() || mImageDir.mkdirs();
                if(isDirectoryCreated){
                    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/YOUR_DIRECTORY", "YOUR_IMAGE.jpg");
                    ImageCapture.OutputFileOptions.Builder outputFileOptionsBuilder =
                            new ImageCapture.OutputFileOptions.Builder(file);
                    imageCapture.takePicture(outputFileOptionsBuilder.build(), Runnable::run, new ImageCapture.OnImageSavedCallback() {
                        @Override
                        public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
                            Bundle params = new Bundle();
                            params.putString("FILE_PATH", file.getPath());
                            Navigation.findNavController(root).navigate(R.id.navigation_edit_image, params);
                        }

                        @Override
                        public void onError(@NonNull ImageCaptureException exception) {
                            exception.printStackTrace();
                        }
                    });
                }

请注意,如果您将 outputFileResults.getSavedUri() 与此方法一起使用,您将始终拥有一个空 uri.

Be aware that if you use outputFileResults.getSavedUri() with this method you will always have a null uri.

这篇关于OnImageSavedCallback 返回的 OutputFileResults 具有无效的 Uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆