about obtaining read and write access to Android external storage

Source: Internet
Author: User

Recently made a small app about the album, need to read the storage device of the Android phone, first need to obtain the system read and write permission, in the online a good find, for the veteran, may be easy, for the rookie, it is a little difficult, in order to learn, especially write some of their own experience to write down, for reference. If there is any improper, please correct me.

Let's start with an introduction to the phone's internal storage and external storage:

The internal storage device does not need to request read and write permission, usually automatically assigned to the phone, when uninstalling the app disappears: use Getfiledirs () to get/data/data/packagename/files/xxx directly.

External storage device generally need to get path need Environment.getexternalstoragedirectory () method to get/storage/emulate/0/, pro-test Xiaomi phone

One, mainly because after Android 6.0 only in the manifest file to write these two permissions list, is not enough, need to write code dynamic request permission

<!--permissions List--
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>

Second, the following is the code, I also find, there are links, mainly to explain, better understanding. The following is a class, the main judgment there is no authorization, no authorization on the authorization
/*
* Borrow the blogger's code for access to external storage 78081114
* */

/**
* About application authorization
* Only need to apply once in the main interface
* In other sub-activity, automatic authorization
* */
public class Permissionutils {
This is the permission to apply
private static string[] Permissions_camera_and_storage = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA};

/**
* Resolves an issue where Android version 6.0 or above cannot read external storage permissions
*
* @param activity
* @param requestcode
* @return
*/
public static Boolean ISGRANTEXTERNALRW (activity activity, int requestcode) {
if (Build.VERSION.SDK_INT >= build.version_codes. M) {

int storagepermission = activity.checkselfpermission (Manifest.permission.WRITE_EXTERNAL_STORAGE);
int camerapermission = activity.checkselfpermission (Manifest.permission.CAMERA);
Check for permissions, and if you don't have permission, you need to apply
if (storagepermission! = packagemanager.permission_granted | |
Camerapermission! = packagemanager.permission_granted) {
Request permission
Activity.requestpermissions (Permissions_camera_and_storage, Requestcode);
returns FALSE. Description not Authorized
return false;
}
}
Description already authorized
return true;
}
}


Third, how to use it in specific activity

1. Call the above class to request permission, in the OnCreate function call
Get authorization for this if: else can not write, direct write permission to request code
if (PERMISSIONUTILS.ISGRANTEXTERNALRW (this, 1)) {

}
2.onRequestPermissionResult is a callback to the Activity.requestpermissions () function, which is handled according to the request code. If you do not have authorization, you can use it naturally. Only need to apply for permission in the activity, other sub-activity will be automatically authorized. This authorization opens a request permission page at the beginning and clicks Allow to get permission
The result of getting permission processing
@Override
public void Onrequestpermissionsresult (int requestcode, string[] permissions, int[] grantresults) {
Switch (requestcode) {
Case 1:
if (grantresults[0] = = packagemanager.permission_granted) {
Verify if permissions are obtained and if the external storage is open, a toast prompt is granted
String sdcard = Environment.getexternalstoragestate ();
if (Sdcard.equals (environment.media_mounted)) {
Toast.maketext (This, "granted", Toast.length_long). Show ();
}
} else {
Runonuithread (New Runnable () {
@Override
public void Run () {
Toast.maketext (Mainactivity.this, "buxing", Toast.length_short). Show ();
}
});
}
Break
}
Super.onrequestpermissionsresult (Requestcode, permissions, grantresults);
}

about obtaining read and write access to Android external storage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.