Clicking on ImageView opens a dialog box with a choice of how to add an image to the ImageView (from the gallery or from the camera). I try to implement adding through the camera, but for some reason if I click "Take a Picture" the camera does not open. What's wrong?

 public class MainActivity extends AppCompatActivity { private ImageView img1; static final int CAMERA_REQUEST = 2; static final int REQUEST_CODE_PERMISSION_WRITE_EXTERNAL_STORAGE = 1; static final int REQUEST_CODE_PERMISSION_CAMERA = 2; String dirName = "MyImage"; File currentImageFileForStep = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img1 = findViewById(R.id.img1); img1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showImageSelectionDialog(); } }); } private void selectImageFromCamera() { if ((ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) &&(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) &&(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); currentImageFileForStep = createImageFileFromCamera(); if (currentImageFileForStep != null) { Uri imageUri = FileProvider.getUriForFile(this, "ru.test.testimage.fileprovider", currentImageFileForStep); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, CAMERA_REQUEST); } } else { ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE }, REQUEST_CODE_PERMISSION_CAMERA); } } private void showImageSelectionDialog() { AlertDialog alertDialog = new AlertDialog.Builder(this) .setTitle("Выбор") .setItems(R.array.attachment_variants, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (which == 0) { //electImageFromGallery(); } else if (which == 1) { selectImageFromCamera(); } } }) .create(); if (!isFinishing()) { alertDialog.show(); } } private void createImageFolder() { File direct = new File(Environment.getExternalStorageDirectory() + File.separator + dirName); if (!direct.exists()) { File wallpaperDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + dirName); wallpaperDirectory.mkdirs(); } } private String createImageFileName() { String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date()); String fileName = "img_" + timeStamp + ".jpg"; return fileName; } @Nullable private File createImageFileFromCamera() { String filename = createImageFileName(); createImageFolder(); File image = new File(dirName, filename); try { if (image.createNewFile()) { return image; } } catch (IOException e) { e.printStackTrace(); } return null; } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case REQUEST_CODE_PERMISSION_CAMERA: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { selectImageFromCamera(); } else { Toast.makeText(this, "Нет прав на работу с камерой", Toast.LENGTH_SHORT).show(); } break; } } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (img1 != null) { Bitmap bitmap = null; switch(requestCode) { case CAMERA_REQUEST: if(resultCode == RESULT_OK) { bitmap = BitmapFactory.decodeFile(currentImageFileForStep.getAbsolutePath()); img1.setImageBitmap(bitmap); img1.setAdjustViewBounds(true); img1.setScaleType(ImageView.ScaleType.CENTER_CROP); } } } } } 

Permissions added to manifest

 <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

    2 answers 2

    You request only the rights to the camera, but you also need to access the repository.

    Try this:

     if ((ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) &&(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) &&(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) { .... } else { ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE }, REQUEST_CODE_PERMISSION_CAMERA); } 
    • Remade, did not help. This is not the case; there is something else that is not what I am doing - Natalya Sergeevna

    You have a camera opening by condition: if (currentImageFileForStep != null)

    currentImageFileForStep defined by the method (line above):

     currentImageFileForStep = createImageFileFromCamera(); 

    which most likely returns NULL

    Go to the method, and see the line: File image = new File(dirName, filename); In dirName this is a relative path, not a complete path, as a result of which creation does not create a file in try , and the method returns NULL .
    ps Such errors are easily caught in logcat - I recommend looking in there during the development process.

    • Already made thanks. - Natalia Sergeevna