2017-01-29 17 views
1

AndroidManifest.xmlにWRITE_EXTERNAL_STORAGE、android.permission.CAMERA、READ_EXTERNAL_STORAGEなどの使用権限を追加しました。Android Runtime Permissions(許可が拒否されました)

私はNexus6(API 24)に私のアプリを実行したとき、それは私に次のエラーがスローされました:ここ

java.io.IOException: Permission denied

は私のコードです:

/** 
* A simple {@link Fragment} subclass. 
*/ 
public class HomeProfileFragment extends Fragment { 

    public static final int REQUEST_IMAGE_CAPTURE = 1; 

    private String imageFilePath; 

    public HomeProfileFragment() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_home_profile, container, false); 
     ///################# 
     String[] perms = {"android.permission.RECORD_AUDIO", "android.permission.CAMERA"}; 

     File imageFile = null; 
     try{ 
      String state = Environment.getExternalStorageState(); 
      if (Environment.MEDIA_MOUNTED.equals(state)) { 
       imageFile = createImageFile(); 
       imageFilePath = imageFile.getAbsolutePath(); 
      } else { 
       /// Throw an error 
      } 
     }catch (IOException e){ 
      e.printStackTrace(); 
     } 

     int permsRequestCode = 200; 

     requestPermissions(perms, permsRequestCode); 
     ///################# 


     if (imageFile != null){ 
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile)); 

      if(takePictureIntent.resolveActivity(getActivity().getPackageManager())!=null){ 
       startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE); 

      } 
     } 
     return view; 
    } 

    ///################## 
    public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){ 

     switch(permsRequestCode){ 

      case 200: 

       boolean audioAccepted = grantResults[0]== PackageManager.PERMISSION_GRANTED; 

       boolean cameraAccepted = grantResults[1]==PackageManager.PERMISSION_GRANTED; 

       break; 

     } 

    } 

    ///################## 

    private File createImageFile()throws IOException { 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     String fileName = "JPEG_" + timeStamp + "_"; 

     File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 

     File image = File.createTempFile(fileName, ".jpg", storageDir); 
     return image; 

    } 


    private void addPhotoToGallery(String filepath){ 
     Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
     File f = new File(filepath); 
     Uri uri =Uri.fromFile(f); 
     mediaScanIntent.setData(uri); 

     getActivity().sendBroadcast(mediaScanIntent); 

    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == Activity.RESULT_CANCELED){ 
     } if (requestCode == Activity.RESULT_CANCELED){ 
      addPhotoToGallery(imageFilePath); 
     } 
    } 
    ///################################# 
    private boolean shouldAskPermission(){ 

     return(Build.VERSION.SDK_INT> Build.VERSION_CODES.LOLLIPOP_MR1); 

    } 
} 

どのように私は許可カメラランタイムを使用することができますそれが開く前に?

+2

あなたの 'perms'配列に記憶権を追加する必要があります –

答えて

0

あなたはそれがマニフェストでなければならないので、インストーラがアプリにこれらの権限を付与するユーザーを求めることができマニフェスト

<uses-feature android:name="android.hardware.camera2" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

でユーザー権限を宣言する必要があります。

関連する問題