CJR
CJR

Reputation: 3572

Android: nothing happens on request runtime permission?

I want to request permission to read external storage on runtime. When I click on a Button the app should ask for permission, but the dialog does not show up when button is clicked. Code (This is from a 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);
    }

}

Anyone knows why the dialog requesting permission does not show up here?

Upvotes: 1

Views: 523

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006574

For the dialog to appear, you need to:

  • Have a targetSdkVersion of 23 or higher

  • Have the permission(s) that you are requesting also in the manifest, with <uses-permission> elements

  • Be running on Android 6.0 or higher

Upvotes: 1

Related Questions