Issues with requesting permissions Marshmallow

前端 未结 1 1250
南笙
南笙 2020-12-22 06:49

I\'m trying to make a basic camera application that can access the saved photo from the gallery (needed as part of another app but due to problems I\'ve been having I am dev

相关标签:
1条回答
  • 2020-12-22 07:17

    Try this

    public void onLaunchCamera(View view) {
    
        //btn = (Button) findViewById(R.id.button);
        if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
             if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
                checkPermission();
            }
            else {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName)); // set the image file name
    
                if (intent.resolveActivity(getPackageManager()) != null) {
                // Start the image capture intent to take photo
                startActivityForResult(intent, 0);
                }
            }
        } else {
            Toast.makeText(MainActivity.this, "No Camera",
                    Toast.LENGTH_LONG).show();
        }
    }
    
    private void checkPermission() {
                if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
                        Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED) {//Can add more as per requirement
    
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA},
                            123);
    
                } else {
    
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,getPhotoFileUri(photoFileName)); // set the image file name     
    
               if (intent.resolveActivity(getPackageManager()) != null) {
               // Start the image capture intent to take photo
               startActivityForResult(intent, 0);
                 }
              }
    

    And make sure you have set proper version in your build.gradle

        **compileSdkVersion 23
        buildToolsVersion "23.0.2"**
    
        defaultConfig {
            applicationId "your_package_name"
            minSdkVersion 15
            **targetSdkVersion 23**
            versionCode 1
            versionName "1.0"
            multiDexEnabled true
    
        }
    
    0 讨论(0)
提交回复
热议问题