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
StringBuilder bodyStr=new StringBuilder();
bodyStr.append(data1Str.toString()).append(",").append(data2Str.toString()).append(",").append(data3Str.toString()).append(",").append(data4Str.toString()).append(",").append(data5Str.toString()).append(",").append(data22Str.toString()).append(",").append(data23Str.toString()).append(lineSep);;
String bodytextStr=bodyStr.toString();
boolean hasPermission = (ContextCompat.checkSelfPermission(data_entry.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
ActivityCompat.requestPermissions(data_entry.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_WRITE_STORAGE);
}
try {
File myFile = new File(fileName);
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(bodytextStr);
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
Comes back [permission denied]
have set usual permissions in manifest
any ideas where i'm going wrong
Any help Appreciated
Mark
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.
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 :
I suggest that you check this link : https://developer.android.com/training/permissions/requesting.html