Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

How do you request MANAGE_EXTERNAL_STORAGE permission in Android?

From Android Docs, https://developer.android.com/training/data-storage/manage-all-files#all-files-access

"An app can request All files access from the user by doing the following:

Declare the MANAGE_EXTERNAL_STORAGE permission in the manifest.

Use the ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION intent action to direct users to a system settings page where they can enable the following option for your app: Allow access to manage all files."

What I've tried

The only way I know how to request permissions is with ActivityCompat. I've tried:

ActivityCompat.requestPermissions(this, new String[]{Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION},1);

and

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

Neither of which do anything.

The Android docs are extensive but not exactly the most welcoming for newcomers. I understand intents, and know they can be used to switch between activities, but I don't know what an "intent action" is and how it can be used to request permissions

question from:https://stackoverflow.com/questions/65876736/how-do-you-request-manage-external-storage-permission-in-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In Kotlin:

    val uri = Uri.parse("package:${BuildConfig.APPLICATION_ID}")

    startActivity(
      Intent(
        Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION,
        uri
      )
    )

(from this sample project)

You are probably used to an implicit Intent (Intent(this, SomeActivity::class.java)). The docs are asking you to use an explicit Intent, one with an action string and, in this case, a Uri. The Uri will have the package scheme and identify your app by its application ID.

That code snippet will start a system-supplied activity that, in theory, will let the user opt into granting your app the MANAGE_EXTERNAL_STORAGE permission.

The Android docs are extensive but not exactly the most welcoming for newcomers

You might wish to consider reading a book or taking a course. Any decent book or course will cover the concept of Intent actions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...