I can not find the file that I do with the camera in the application. I tried to change the path to save, but it does not lead to anything. I would be very grateful for the help. PS In the manifest file, all permissions are written

private void takePicture() { if(cameraDevice == null) return; CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE); try{ CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId()); Size[] jpegSizes = null; if(characteristics != null) jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP) .getOutputSizes(ImageFormat.JPEG); //Capture image with custom size int width = 640; int height = 480; if(jpegSizes != null && jpegSizes.length > 0) { width = jpegSizes[0].getWidth(); height = jpegSizes[0].getHeight(); } final ImageReader reader = ImageReader.newInstance(width,height,ImageFormat.JPEG,1); List<Surface> outputSurface = new ArrayList<>(2); outputSurface.add(reader.getSurface()); outputSurface.add(new Surface(textureView.getSurfaceTexture())); final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); captureBuilder.addTarget(reader.getSurface()); captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO); //Check orientation base on device int rotation = getWindowManager().getDefaultDisplay().getRotation(); captureBuilder.set(CaptureRequest.JPEG_ORIENTATION,ORIENTATIONS.get(rotation)); file = new File(Environment.getDataDirectory()+"/My files/"+UUID.randomUUID().toString()+".jpg"); ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() { @Override public void onImageAvailable(ImageReader imageReader) { Image image = null; try{ image = reader.acquireLatestImage(); ByteBuffer buffer = image.getPlanes()[0].getBuffer(); byte[] bytes = new byte[buffer.capacity()]; buffer.get(bytes); save(bytes); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { { if(image != null) image.close(); } } } private void save(byte[] bytes) throws IOException { OutputStream outputStream = null; try{ outputStream = new FileOutputStream(file); outputStream.write(bytes); }finally { if(outputStream != null) outputStream.close(); } } }; reader.setOnImageAvailableListener(readerListener,mBackgroundHandler); final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() { @Override public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) { super.onCaptureCompleted(session, request, result); Toast.makeText(MainActivity.this, "Saved "+file, Toast.LENGTH_SHORT).show(); createCameraPreview(); } }; cameraDevice.createCaptureSession(outputSurface, new CameraCaptureSession.StateCallback() { @Override public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) { try{ cameraCaptureSession.capture(captureBuilder.build(),captureListener,mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) { } },mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } 
  • runtime permissions are distributed? save (byte [] bytes) work? just even if the bytes are gone, then from Java file = new File () will create it if the path is available (by the way it will check bool bool), try first to create the file before writing the data to it, as it turns out you can drag in your footcloth, if that write it down - Shwarz Andrei

1 answer 1

Do you have no errors in the logs and they do not make it clear that the file was not created and not recorded? Environment.getDataDirectory() is a utility folder and should not be written to it, even if it works.
Replace with Environment.getExternalStoragePublicDirectory(String type) , for example.
In his documentation there is just an example of saving an image with a MediaScanner alert, besides that it would appear in the gallery.

When you want to write to your subfolder, you must first create it using the mkdir() or mkdirs() method for several nested mkdirs() at once.

And finally, starting with Android 6.0 (API level 23), permissions in the manifest are not enough - some (read / write including) need to be checked and queried at run time.
Documentation: Request App Permissions