I'm trying to write a large multidimensional array into an ascii file in order to debug my program. (My phone has Android 8.0.0)
I added this line to my AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
The function is called within a subclass, so no context
is available here:
public void writeAsAscii(float [][][][] labelProbArray, int Nx, int Ny, int Nz ) {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
Log.i("State","External storage is writable");
} else{
Log.i("State","Error: External storage NOT WRITABLE");
}
File path = Environment.getExternalStorageDirectory();
File file = new File(path, "MyFile.txt");
try {
FileWriter writer = new FileWriter(file, true);
for (int i = 0; i < Nx; i++) {
for (int j = 0; j < Ny; j++) {
for (int k = 0; k < Nz; k++){
writer.write(String.valueOf(labelProbArray[0][i][j][k])+" ");
}
writer.write("\n"); // write new line
}
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
I'm following the advice given in related posts, but the writing fails. In debug mode I can see that path
is set do /storage/emulated/0/MyFile.txt
, but I cannot find the file anywhere on my phone that I use for debugging. So the file is probably never created. The try
block is failing and the catch
block reports:
java.io.FileNotFoundException: /storage/emulated/0/MyFile.txt (Permission denied)
I'm not sure what is going wrong. Is the file not created? Do I need to add more Permissions? A short clear toy example how to write custom ascii files anywhere in the code would be nice, as it is crucial for debugging large arrays.