Reputation: 574
It seems that whenever I use MkDirs or save the file by itself (without making a directory), it fails. The app does have permission to write and read storage, as this was checked in the main activity and the activity will not even start if permission is not granted. I am running this on Android 8.1.0 on a Pixel 2 XL. Using DIRECTORY_DOWNLOADS, and others like that have also not worked. It feels like a permissions or other storage issue, but I am not so sure.
public void onInput(MaterialDialog dialog, CharSequence input) {
inputNormal = textContent.getText().toString();
File rootFolder = new File(Environment.getExternalStorageDirectory().toString(), "Sorbet");
if (!rootFolder.exists()) {
rootFolder.mkdirs();
}
try {
File file = new File(rootFolder, input.toString()+".txt");
BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
writer.write(inputNormal);
writer.newLine();
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
For context, here is the code that I use to check permissions. This is fully working, as the dialog shows to grant permission, and the permission shows as granted in the system settings for the app.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.READ_EXTERNAL_STORAGE);
if (permissionCheck == PackageManager.PERMISSION_DENIED) {
Snackbar errorBar = Snackbar.make(findViewById(R.id.content_main), getString(R.string.snackbar_error),
Snackbar.LENGTH_LONG)
.setAction(getString(R.string.allow_access), new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
STORAGE_PERMISSION_RC);
}
});
errorBar.show();
}
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
startActivity(NoteCreateIntent);
}
}
});
}
else{
fab.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
startActivity(NoteCreateIntent);
}
});
}
Upvotes: 1
Views: 1192
Reputation: 574
The issue has been fixed. This was a permission issue with Android. In order to write files, you have to explicitly ask for WRITE_EXTERNAL_STORAGE, something that I dismissed.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
val permissionCheck = ContextCompat.checkSelfPermission(applicationContext,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
//Granted Permission
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
val sorbetDirectory = File(Environment.getExternalStorageDirectory().path + "/Sorbet")
sorbetDirectory.mkdirs()
}
//Denied Permission
if (permissionCheck == PackageManager.PERMISSION_DENIED) {
MaterialDialog.Builder(this)
.title(com.marlonjones.sorbet.R.string.titleP)
.content(com.marlonjones.sorbet.R.string.perm_content)
.positiveText(com.marlonjones.sorbet.R.string.ok)
.onPositive { dialog, which ->
ActivityCompat.requestPermissions(this@MainActivity,
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
STORAGE_PERMISSION_RC)
}
.show()
}
}
Upvotes: 0
Reputation: 7367
I would try, just temporarily, switching the folder that you are putting files in from
Environment.getExternalStorageDirectory()
to
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
This directory (along with the music, videos, etc. directories) always seems to be well-defined, whereas the getExternalStorageDirectory
varies from device to device.
I would suspect that getExternalStorageDirectory
is just not returning the correct value. You should try saving in a different directory to ensure that the error is not due to some other part of your code.
If you can't save to the downloads
folder, then the error is likely in some other part of your code.
If you can save to the downloads
folder, then the error is most likely due to something weird with the path returned by getExternalStorageDirectory()
.
Two more suggestions:
Log the value of rootFolder. You need to triple check to make sure that it is what you think it should be. Please edit the question with this value.
mkdirs
returns a boolean indicating whether the directory was made. That command CAN fail, typically if there is a permissions issue. You should also log the return value of that command. If, for instance, mkdirs
returns false, then you know where the problem lies.
Upvotes: 1