Reputation: 746
I have written an Android App which will read a txt file in onCreate of the Main Activity. It works okay. However I found that on Android 6, when I open the app for the first time, it required me to allow the permission for reading file from storage. As a result the app cannot read the file for the very first time because it is needed to wait for the user's action.
How can I read the file immediately after the user grant the permission?
I asked for permission for Android 6 as 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.
Upvotes: 1
Views: 96
Reputation: 331
I faced this problem in another case to get the location of user but i couldn't till i make a restart.
I handled it like the blow. the problem is when you ask permission in onCreated method when you open the activity or fragment it first creats the view and then ask for the permission so when you allow the app to access the permission request the code already executed so it wont work till next restart but android solve this problem with onRequestPermissionsResult
method which will wait till the user decide to allow the request or not then it will execute the method. move your permission check inside onActivityCreated
:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) // grant the access from user when the activity created
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, // if the permission wasn't granted so ask for permission
PERMISSION_ACCESS_FINE_LOCATION);
} else { // if it was granted so get the location
getLocation();
}
}
then you need to do the things if you want it immediately 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 is cancelled, the result arrays are empty.
for (int res : grantResults) {
allowed = allowed && (res == PackageManager.PERMISSION_GRANTED);
}
break;
default:
allowed = false;
break;
}
if (allowed) {
getLocation();
} else {
Toast.makeText(getContext(),"You need to 'Enable' the location service", Toast.LENGTH_SHORT).show();
}
}
in this case you dont need to restart your app it will work very first time i did it this way and work perfectly hope it helps you
Upvotes: 1