0

I am working with a Samsung Galaxy Tab S tablet and try to grant permission to write on the external storage at runtime.

However the dialog to request the permission does not show up. Instead in the code onRequestPermissionResult will be executed instantly with the a PERMISSION_DENIED result. I even tried granting the storage permission manually in the application settings on the device, but even then the result will be PERMISSION_DENIED (grantResults is -1).

My code looks like this:

public class MainActivity extends AppCompatActivity { public static final int REQUEST_STORAGE_PERMISSION = 225; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (Build.VERSION.SDK_INT >= 23) { if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { process(); } else { requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE_PERMISSION); } } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case REQUEST_STORAGE_PERMISSION: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_LONG).show(); process(); } else { Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_LONG).show(); } } } public void process() { //... } } 

When starting the application, it will instantly show the "Permission Denied"-Toast, without showing me any dialog before. That happens, even if i grant the Storage-permission manually in the app settings of the tablet.

My manifest-file also contains the permission at the right place (outside of the application tags) as suggested in similar threads.

I tried reinstalling the app and even reset the tablet already, but nothing seems to work.

2 Answers 2

1

instead of:

 requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE_PERMISSION); 

it works for fragments

You need to use this: As you are using AppCompatActivity

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE_PERMISSION); 

EDIT

As you have multiple project module: you need to add permission in both of those Manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks alot. So i changed the line in my main project and nothing changed. Then i went ahead, created a new project and copied the code and it worked. So i kept looking for the reason why it didnt work in my main project. The project contains 2 additional modules and when i comment out the lines compile project(':module1') and compile project(':module2') in my gradle file it suddenly works. However when i comment them back in, the request dialog wont show up anymore.
Take a look at this Answer
Okay, i actually fixed that last mistake now. The problem was, that only the manifest-file of my main module contained the necessary uses-permission line. I did not know, all modules need to contain this line too. So i added <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> to the manifest of all my modules and it works now. thanks alot!
may be you checked checkbox in the permission dialog.
0

Do this thing

private boolean RequestPermissions() { int camera = ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.CAMERA); int storage = ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE); List<String> listPermissionsNeeded = new ArrayList<>(); if (camera != PackageManager.PERMISSION_GRANTED) { listPermissionsNeeded.add(CAMERA); } if (storage != PackageManager.PERMISSION_GRANTED) { listPermissionsNeeded.add(WRITE_EXTERNAL_STORAGE); listPermissionsNeeded.add(READ_EXTERNAL_STORAGE); } if (!listPermissionsNeeded.isEmpty()) { ActivityCompat.requestPermissions(getActivity(), listPermissionsNeeded.toArray (new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS); return false; } return true; 

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.