user1796624
user1796624

Reputation: 4007

How to turn on storage permissions?

I have set up:

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

in the manifest. But the app has still problems writing to external storage. Looking in application manager the storage permission for the app is thinned off. How to enable it? And why is this happening this now? Is it because of a new android version?

 Settings->Applications->Application Manager-><App name>->Permissions

Upvotes: 0

Views: 4129

Answers (1)

Divyesh Patel
Divyesh Patel

Reputation: 2576

try this to get runtime permission:

if (ContextCompat.checkSelfPermission(sendNotificationActivity.this,Manifest.permission.READ_EXTERNAL_STORAGE)
                                != PackageManager.PERMISSION_GRANTED)
                        {
                            if (!ActivityCompat.shouldShowRequestPermissionRationale(sendNotificationActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(sendNotificationActivity.this);
                                alertBuilder.setCancelable(true);
                                alertBuilder.setTitle("Permission necessary");
                                alertBuilder.setMessage("Storage Permission is needed for sending Image.");
                                alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                                    public void onClick(DialogInterface dialog, int which) {

                                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                                                Uri.parse("package:" + activity.getPackageName()));
                                        intent.addCategory(Intent.CATEGORY_DEFAULT);
                                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                        activity.startActivity(intent);
                                    }
                                });
                                AlertDialog alert = alertBuilder.create();
                                alert.show();

                            } else {
                                ActivityCompat.requestPermissions(sendNotificationActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 541);
                            }
                        }

                        else {                      
                            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            if (intent.resolveActivity(getPackageManager()) != null) {                            
                                startActivityForResult(intent, 2);
                            }
                        }

And override this:

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {

        switch (requestCode) {

            case 541: {
                for( int i = 0; i < permissions.length; i++ ) {
                    if( grantResults[i] == PackageManager.PERMISSION_GRANTED ) {


                        Log.d("Permissions CAMERA", "Permission Granted: " + permissions[i]);
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName));
                        if (intent.resolveActivity(getPackageManager()) != null) {                           
                            startActivityForResult(intent, 1);
                        }


                    } else if( grantResults[i] == PackageManager.PERMISSION_DENIED ) {
                        Log.d( "Permissions CAMERA", "Permission Denied: " + permissions[i] );
                    }
                }
                break;
            }
             default: {
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
            break;
            }
            }

Upvotes: 1

Related Questions