This code works fine with the camera in android versions below 6.0. But with version 6.0 gives an error in the use of the camera. How can I fix this error?

public class MainActivity extends AppCompatActivity { private final String TAG = this.getClass().getName(); ImageView ivCamera, ivGallery, ivUpload, ivImage; CameraPhoto cameraPhoto; GalleryPhoto galleryPhoto; final int CAMERA_REQUEST = 13323; final int GALLERY_REQUEST = 22131; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); cameraPhoto = new CameraPhoto(getApplicationContext()); galleryPhoto = new GalleryPhoto(getApplicationContext()); ivImage = (ImageView)findViewById(R.id.ivImage); ivCamera = (ImageView)findViewById(R.id.ivCamera); ivGallery = (ImageView)findViewById(R.id.ivGallery); ivUpload = (ImageView)findViewById(R.id.ivUpload); ivCamera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { startActivityForResult(cameraPhoto.takePhotoIntent(), CAMERA_REQUEST); cameraPhoto.addToGallery(); } catch (IOException e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "Something Wrong while taking photos", Toast.LENGTH_SHORT).show(); } } }); ivGallery.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivityForResult(galleryPhoto.openGalleryIntent(), GALLERY_REQUEST); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == RESULT_OK){ if(requestCode == CAMERA_REQUEST){ String photoPath = cameraPhoto.getPhotoPath(); try { Bitmap bitmap = ImageLoader.init().from(photoPath).requestSize(512, 512).getBitmap(); ivImage.setImageBitmap(bitmap); } catch (FileNotFoundException e) { Toast.makeText(getApplicationContext(), "Something Wrong while loading photos", Toast.LENGTH_SHORT).show(); } } else if(requestCode == GALLERY_REQUEST){ Uri uri = data.getData(); galleryPhoto.setPhotoUri(uri); String photoPath = galleryPhoto.getPath(); try { Bitmap bitmap = ImageLoader.init().from(photoPath).requestSize(512, 512).getBitmap(); ivImage.setImageBitmap(bitmap); } catch (FileNotFoundException e) { Toast.makeText(getApplicationContext(), "Something Wrong while choosing photos", Toast.LENGTH_SHORT).show(); } } } } } 

Android Manifest

Android Monitor

Main Activity

    2 answers 2

     Permission denied... 

    This is not casual. Starting with Android 6.0 and above, you need to handle such permission , asking the user to give the green light to use the camera or not.

     private static final int PERMISSION_REQUEST = 1; if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST);//выводит диалог, где пользователю предоставляется выбор }else{ //продолжаем работу или вызываем метод или класс } 

    Update Answer:

     public class MainActivity extends AppCompatActivity { private static final int PERMISSION_REQUEST = 1; .... ivCamera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST);//выводит диалог, где пользователю предоставляется выбор }else{ try { startActivityForResult(cameraPhoto.takePhotoIntent(), CAMERA_REQUEST); cameraPhoto.addToGallery(); } catch (IOException e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "Something Wrong while taking photos", Toast.LENGTH_SHORT).show(); } } } }); 
    • Many thanks for the reply. Can you please tell me. Where in the Main Activity should your code be integrated into your - Kim Vladislav
    • @KimVladislav was glad to help! :) I updated my answer - iFr0z
    • Sorry, I don't seem to be following your instructions correctly. But failed to execute the command. i.stack.imgur.com/HjeyD.png - Kim Vladislav
    • @KimVladislav is not "this" then, but "MainActivity.this". Even the studio tells you ... this (anonymus);) - iFr0z

    Since the sixth version of the android permission request mechanism has changed: http://developer.android.com/training/permissions/requesting.html

    In essence, you need to call runtime permissions if necessary by the requestPermissions function