I am not able to read from or write to my Android device. There were similar questions asked on the same topic (I have implemented the code) but they all seem to crash my app.
Button btnUpload = (Button) findViewById(R.id.btnUpload);
File created = new File(Environment.getExternalStorageDirectory().getPath()
+ File.separator + "MyNewFolder" + File.separator + "created");
if (created.isDirectory()) {
File[] contents = created.listFiles();
//Should not trigger as directory check is in place
if (contents == null) { //<--not adding this section causes a crash
btnUpload.setEnabled(false);
}
//Check to see if the number of files is 0
else if (contents.length == 0) {
btnUpload.setEnabled(false);
}
//All checks pass. Enable button
else {
btnUpload.setEnabled(true);
}
}
- Manifest file has the "WRITE_EXTERNAL_STORAGE" permission added
- Ensured the phone (Nexus 5, with marshmallow) is on debugging mode and "File transfer" mode is selected.