I want to create a new file in external storage if that file doesn't exist already.
I've already read similar questions in SO and have WRITE_EXTERNAL_STORAGE
permission in my manifest.
Testing on GenyMotion emulator with android 5.1 and xperia t with android 4.3 the result is same and I get "open failed: EACCES (Permission denied)" on file.createNewFile()
. I checked in runtime and getExternalStorageState
functoin return value is "MOUNTED".
Note: If I create the file manually, my code works perfectly and reads the content meaning that accessing to external storage is OK.
I although write to external storage another place in my code using getExternalPublicStorage
for saving captured image and it works fine!
File f = Environment.getExternalStorageDirectory();
File config = new File(f, "poinila config" + ".txt");
if (!config.exists()) {
if (!config.createNewFile()) {
// toast that creating directory failed
} else {
writeDefaultIpPort();
}
}
Edit: path string is "/storage/sdcard0/poinila config.txt"
well I think this is the new security feature introduced in android. Runtime permissions. You have to do something like this
int hasReadExternalStoragePermission = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
if(hasReadExternalStoragePermission != PackageManager.PERMISSION_GRANTED)
if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE))
new AlertDialog.Builder(getContext())
.setMessage("You need to Allow access to Images to set a Company Logo")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
requestPermission();
}
}).show();
else
requestPermission();
else
callChooser();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With