Android:请求运行时权限没有任何反应?

时间:2017-04-18 16:26:19

标签: android

我想请求在运行时读取外部存储的权限。当我点击Button应用程序应该请求权限,但单击按钮时对话框不显示。代码(来自Fragment):

    private Button photo;

    //Constants
    private static final int GALLERY_INTENT = 2339;
    private static final int REQUEST_EXTERNAL_STORAGE = 4435;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_profile, container, false);

        photo = (Button) rootView.findViewById(R.id.photoButton);
        photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //start permission check for gallery
                //check if permission is granted
                if(ActivityCompat.checkSelfPermission(getContext(),
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED){
                    //if permission is not granted, ask for permission.
                    ActivityCompat.requestPermissions(getActivity(),
                            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                            REQUEST_EXTERNAL_STORAGE);
                }else{
                    //if permission already granted, start gallery intent.
                    uploadPhotoToFirebase();
                }

            }
        });


        return rootView;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){

            Uri uri = data.getData();

            StorageReference storageReference = FirebaseStorage
                    .getInstance()
                    .getReference("profile_images/"+FirebaseAuth
                            .getInstance()
                            .getCurrentUser()
                            .getUid()
                    );

            storageReference.putFile(uri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                    //File successfully uploaded
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    //File upload not successful
                }
            });

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case REQUEST_EXTERNAL_STORAGE:
                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    //Permission to read external storage GRANTED
                    uploadPhotoToFirebase();

                }else{
                    //Permission to read external storage DENIED
                }
        }
    }

    private void uploadPhotoToFirebase(){
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        startActivityForResult(intent, GALLERY_INTENT);
    }

}

任何人都知道为什么请求权限的对话框没有显示在这里?

1 个答案:

答案 0 :(得分:1)

要显示对话框,您需要:

  • 拥有23个或更高的targetSdkVersion

  • 使用<uses-permission>元素

  • 获取您在清单中请求的权限
  • 在Android 6.0或更高版本上运行