Android 7.1 Write To Text File
Guys New to Nougat come from Jelly bean Trying to write to sdcard a text file I know I now have to request permissions but cant find any code that works Tried the following
Solution 1:
Either you have the permission and can do the try catch block where you write into the file or you have to do it in the onRequestPermissionsResult.
Something like :
publicvoidyourMethod(){
booleanhasPermission= (ContextCompat.checkSelfPermission(data_entry.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
if(hasPermission){
// write
}else{
// ask the permission
ActivityCompat.requestPermissions(data_entry.this,
newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_WRITE_STORAGE);
// You have to put nothing here (you can't write here since you don't// have the permission yet and requestPermissions is called asynchronously)
}
}
@OverridepublicvoidonRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNullint[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// The result of the popup opened with the requestPermissions() method// is in that method, you need to check that your application comes hereif (requestCode == REQUEST_WRITE_STORAGE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// write
}
}
}
I suggest that you check this link : https://developer.android.com/training/permissions/requesting.html
Solution 2:
This is what you need: https://developer.android.com/training/articles/scoped-directory-access.html#accessing
You have to ask the user to give the rights to access it.
Post a Comment for "Android 7.1 Write To Text File"