2016-06-21 6 views
0

ギャラリーから保存された写真にアクセスできる基本的なカメラアプリケーションを作成しようとしています(別のアプリケーションの一部として必要ですが、このチュートリアルは主にこのチュートリアルに従いましたhttps://guides.codepath.com/android/Accessing-the-Camera-and-Stored-Media権限を要求する際の問題Marshmallow

マシュマロで権限がどのように動作するのか、下位互換性が必要なためにクラッシュすることに気がついたので、私は許可要求を実装するチュートリアルに従いました。実際にアプリを使用することができます。

これは私が現在数時間試してみたものです。私はマニフェストのパーミッションを追加しましたが、これはかなり標準的なものなので、これらをコピーして貼り付ける手間はありませんでした。これは、Storageというグループが存在しないため、現在test()メソッドでクラッシュしています。その行がコメントアウトされていると、権限を拒否しても、権限が拒否されただけです(マシュマロデバイスであろうとなかろうと、権限が拒否されます)。率直に言って、私は今や失われている。私が必要とするのは、ボタンをクリックして起動するonLaunchCameraメソッドでカメラを起動する前に、外部ストレージの読み書きとカメラへのアクセスの許可を得ることです。あなたが与えることができるどんな助けも大いに感謝されるでしょう。

private boolean cameraPermissionsCheck() { 
    return ContextCompat.checkSelfPermission(this, Manifest.permission_group.CAMERA) == PackageManager.PERMISSION_GRANTED; 
} 

private boolean storagePermissionsCheck() { 
    return ContextCompat.checkSelfPermission(this, Manifest.permission_group.STORAGE) == PackageManager.PERMISSION_GRANTED; 
} 

private void requestPermissions() { 
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission_group.CAMERA, Manifest.permission_group.STORAGE}, 123); 
} 

private void test() { 
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission_group.STORAGE)) { 
     //was a toast notification here 
     requestPermissions(); 
    } else { 
     requestPermissions(); 
    } 
} 
@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    if (requestCode == 123 
      && grantResults.length > 0 
      && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
     Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show(); 
    } else { 
     Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show(); 
    } 
} 

public void onLaunchCamera(View view) { 

    //btn = (Button) findViewById(R.id.button); 
    if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){ 
     if(!cameraPermissionsCheck() || !storagePermissionsCheck()){ 
      test(); 
     } 
     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(); 
    } 
} 
+0

ポストマニフェスト –

+0

はそれがかもしれこれを試してみてください作業をhttpに適切なバージョンを設定していることを確認してください:// stackoverflow.com/a/41221852/5488468 –

答えて

1

この

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); 
      } 
      } 

を試してみて、あなたはあなたの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

お取り扱い、ありがとうございます! – markeh21

+0

@ markeh21あなたは大歓迎です:) – Nisarg

関連する問題