Android: File did not load when first opening the App -


i have written android app read txt file in oncreate of main activity. works okay. found on android 6, when open app first time, required me allow permission reading file storage. result app cannot read file first time because needed wait user's action.

how can read file after user grant permission?

i asked permission android 6 below:

        if (build.version.sdk_int >= build.version_codes.m) {             if (contextcompat.checkselfpermission(this, manifest.permission.write_external_storage)                     != packagemanager.permission_granted) {                 requestpermissions(new string[]{manifest.permission.write_external_storage},                         mainactivity.request_permission_write_external_storage);             }              if (contextcompat.checkselfpermission(this, manifest.permission.read_external_storage)                     != packagemanager.permission_granted) {                 requestpermissions(new string[]{manifest.permission.read_external_storage},                         mainactivity.request_permission_read_external_storage);             }         }  

thank you.

i faced problem in case location of user couldn't till make restart.

i handled blow. problem when ask permission in oncreated method when open activity or fragment first creats view , ask permission when allow app access permission request code executed wont work till next restart android solve problem onrequestpermissionsresult method wait till user decide allow request or not execute method. move permission check inside onactivitycreated:

@override public void onactivitycreated(@nullable bundle savedinstancestate) {     super.onactivitycreated(savedinstancestate);     if (contextcompat.checkselfpermission(getactivity(), manifest.permission.access_fine_location) // grant access user when activity created             != packagemanager.permission_granted) {         requestpermissions(new string[]{manifest.permission.access_fine_location}, // if permission wasn't granted ask permission                 permission_access_fine_location);     } else { // if granted location         getlocation();     } } 

then need things if want inside onrequestpermissionsresult method:

 @override     public void onrequestpermissionsresult(int requestcode, string permissions[], int[] grantresults) {         boolean allowed = true;         switch (requestcode) {             case permission_access_fine_location:                 // if request cancelled, result arrays empty.                 (int res : grantresults) {                     allowed = allowed && (res == packagemanager.permission_granted);                 }                 break;             default:                 allowed = false;                 break;         }         if (allowed) {             getlocation();         } else {             toast.maketext(getcontext(),"you need 'enable' location service", toast.length_short).show();         } } 

in case dont need restart app work first time did way , work hope helps you


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -