Right now I am currently creating an app for myself that appends data to a handful of files. But when I try to create the files -- or indeed, open them at all --, the program throws java.io.IOException: Operation not permitted
.
As you can see, storage permissions are granted. (This app is exclusively for myself, so I'm not too worried about what it would be like for other users.)
Examining the filesystem directly through the phone itself or through my computer does not show these files.
Note: creating the directories (i.e. dir.mkdirs();
) does not throw an error, even if I delete the directories beforehand. Handling the files, however, does throw errors.
I am running Android 11 and my Android Studio is set to work in Lollipop.
MainActivity.java
:
public class MainActivity extends AppCompatActivity {
public static File dir = new File(new File(Environment.getExternalStorageDirectory(), "bleh"), "bleh");
@Override protected void onCreate(Bundle savedInstanceState) {
//bleh
dir.mkdirs();
dir.setWritable(true)
addButtons();
}
public void addButtons() throws IOException {
Option.A.init(R.id.A);
Option.B.init(R.id.B);
Option.C.init(R.id.C);
Option.D.init(R.id.D);
Option.E.init(R.id.E);
Option.F.init(R.id.F);
// This is beyond the scope of my
// question, but if you could also
// show me a better way to do this,
// that would be great.
}
}
Option.java
:
public enum Option {
A, B, C, D, E, F, ;
public File data;
public PrintWriter printer;
Option() {
data = new File(MainActivity.dir, name().toLowerCase() + ".data");
}
public void init(int buttonid) {
try {
data.createNewFile();
printer = new PrintWriter(data);
// Both of these lines throw this exception.
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
}
}