Question:
Hello. I'm trying to write an android application (6.0 in this case). There was a problem during implementation: You need to open a file that was not created by the application. Moving the file to the right directory (namely, the application directory) is not a problem. The problem is that the application can only open the file that was previously created by this application. When I try to open any other files, I get Permission denied. Moreover, the permissions for reading / writing from the card are set in the manifest (as if this helps), in the phone all the checkboxes on the permissions are set. Is there any way to open such a file for reading?
void writeFile() {
try {
// отрываем поток для записи
BufferedReader br = new BufferedReader(new InputStreamReader(
openFileInput(FILENAME)));
String line = "";
String FileContent = "";
String NL = System.getProperty("line.separator");
while ((line = br.readLine()) != null) {
FileContent = FileContent + line + NL;
}
br.close();
FileContent = FileContent.replaceAll("(H|h)(E|e)(L|l)(L|l)(O|o)",
"1234");
System.out.println(FileContent);
BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME));
bw.write(FileContent);
bw.close();
Log.d(LOG_TAG, "Файл записан");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Answer:
I tried to read files, everything worked fine before I tested it on a 6.0 device. Since version 6.0, the following feature is introduced: https://developer.android.com/training/permissions/requesting.html
if((ContextCompat.checkSelfPermission(Context ctx,
Manifest.permission.WRITE_EXTERNAL_STORAGE) ) != 0) {
ActivityCompat.requestPermissions(Activity act,
new String[]Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}