2016-06-21 80 views
0

我試圖做一個基本的相機應用程序,可以訪問從庫中保存的照片(需要作爲另一個應用程序的一部分,但由於我一直有問題,我正在開發這個空白項目),並主要關注本教程https://guides.codepath.com/android/Accessing-the-Camera-and-Stored-Media請求權限的問題Marshmallow

當然後意識到它會因爲權限在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