简体   繁体   中英

Does Anyone knows Where this permission come from and how to grant this programmatically in Android?

I want to create a file explorer, after a lot searching, I found out that I can't Modify, delete or create files on SD card without SAF(Storage Access Framework) on API 25 and higher.Then I installed Some File Manager for Testing how do They Work. All of Them use SAF except Xiaomi File Manager. Xiaomi's only grants a permission. I captured some screenshots. enter image description here enter image description here

EDIT 1: I get all my runtime permissions.

thanks for reading:)

Android defines basically three types of permissions: Normal Permissions Signature Permissions Dangerous Permissions

Both Normal and Dangerous permissions must be defined in the Manifest file. But only Dangerous permissions are checked at runtime, Normal permissions are not.

Normal Permissions Some permissions are automatically granted to the application. Just we need to declare those permissions in AndroidManifest.xml and it will work fine. Those permissions are called Normal Permissions. An example of a normal permission is INTERNET.

list of Normal Permissions:https://developer.android.com/guide/topics/permissions/overview

Signature Permissions A permission that the system grants only if the requesting application is signed with the same certificate as the application that declared the permission. If the certificates match, the system automatically grants the permission without notifying the user or asking for the user's explicit approval.

Dangerous Permissions Some permissions may affect the users private information, or could potentially affect his data or the operation of other application are called Dangerous Permissions. For example, the ability to read the user's contacts is a dangerous permission. Some other examples are CONTACTS, CAMERA,CALENDAR, LOCATION, PHONE, STORAGE, SENSORS, MICROPHONE, etc.

Dangerous permissions must be granted by the user at runtime to the app.

Here is an example for requesting a Dangerous Permissions on runtime:-

First, add permissions to AndroidManifest.xml file

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

then ask for permission from user during runtime, like this:-

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED) {
                if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                }
            }else {
                dispatchTakePictureIntent();
            }
        }

Then override the onRequestPermissionsResult method according to your need

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1){
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
                Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
                    do your stuff...
            }else {
                Toast.makeText(this,"Permission Denied",Toast.LENGTH_SHORT).show();
            }
        }
    }

For more information check out this article: https://medium.com/programming-lite/runtime-permissions-in-android-7496a5f3de55

I found it after too many searching in Stack Overflow, So there is a flag that call that dialog and get the uri of the storage,but this way only works with android 7 to 10. here is answer of my question: question answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM