Can't save captured image in folder using custom camera in android

Mohammed Qadah :

I have simple app with custom camera but when i take picture i can't save the captured image inside Downloads folder in emulator,instead blank folder created i don't know why and where is the mistake in my code. I searched for answers but i didn't find helpful one.

can you help please?

Image Reader

final ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
            List<Surface> outputSurface = new ArrayList<>();
            outputSurface.add(reader.getSurface());
            outputSurface.add(new Surface(textureView.getSurfaceTexture()));
            final CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
            builder.addTarget(reader.getSurface());
            builder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
            int rotation = getActivity().getWindowManager().getDefaultDisplay().getRotation();
            builder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));
            final ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
                @Override
                public void onImageAvailable(ImageReader imageReader) {
                    Image mCapturedImage;
                    mCapturedImage = imageReader.acquireLatestImage();
                    mFile = getActivity().getExternalFilesDir(null);
                    ImageSaver saver = new ImageSaver(mCapturedImage,getActivity().getExternalFilesDir(null));
                    mBackgroundHandler.post(saver);
                }

            };
            reader.setOnImageAvailableListener(readerListener, mBackgroundHandler);
            final CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() {
                @Override
                public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                    super.onCaptureCompleted(session, request, result);
                    showSnackBar("Image saved", Snackbar.LENGTH_LONG);
                    createCameraPreview();
                }
            };

ImageSaver class

 private static class ImageSaver implements Runnable {

        private final File mFile;
        private Image mImage;

        ImageSaver(Image image, File file) {
            mImage = image;
            mFile = file;
        }

        @Override
        public void run() {

            if (mImage != null) {
                ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
                byte[] bytes = new byte[buffer.remaining()];
                buffer.get(bytes);
                FileOutputStream output = null;
                try {
                    SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
                    String format = s.format(new Date());
                    String fileName = format+".jpg";
                    File file = new File(mFile, "image_" + fileName);
                    output = new FileOutputStream(file);
                    output.write(bytes);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (null != output) {
                        try {
                            output.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
Michael Dougan :

Your code is doing the correct thing, so save the image file to the device, but it is saving it to the result of the path returned here:

mFile = getActivity().getExternalFilesDir(null); 

The external file dir is probably pointing to myapp/data/data which is the 'sandbox' data area reserved for files that your app writes to external storage. This is not the same as the Downloads folder, which is saving files down to the shared (re: outside of sandbox) data area on the phone.

If you wanted to save to Downloads, you would probably have to write your images to an external website, then pass the https:// path to a ViewIntent which would display your image in a browser session, and give you the option to download the image from there.

In your code, you have:

mFile = getActivity().getExternalFilesDir(null);

But I don't see where mFile is declared in Image Reader. In your other class, you define it as a File. if mFile is a File, then you should probably be doing something like:

String mPath = getActivity().getExternalFilesDir(null);
File mFile = new File(mPath + "/temp.jpg");

// but I don't think you need to create a file, just use the path
ImageSaver saver = new ImageSaver(mCapturedImage,mPath);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=216484&siteId=1