I'm trying to create a folder on Android to save the photos of the vouchers, but I'm not having success. I already gave permission in Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="<meu.package>">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
And, according to the new Android 7.0 rules, I'm requesting these permissions at runtime.
The code looks like this:
private void CriaPasta() {
pasta = Environment.getExternalStorageDirectory().getPath() + "/Android/data/" + getApplicationContext().getPackageName() + "/comprovantes/";
File folder = new File(pasta);
if (!folder.exists()) {
if (!folder.mkdir()) ;
Toast.makeText(this, pasta + " não pode ser criada.", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case WRITE_EXTERNAL_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
CriaPasta();
}
}
}
}
Here in the UpdateView method of this activity I check if I already have permission and try to create the folder if it does not exist. Here the flow goes straight to the CreatePaste routine because that permission has already been requested at the main entry of the program.
private void UpdateView() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE);
} else {
CriaPasta();
}
}
But in the CreateCreate routine the message saying that the folder can not be created occurs every time.
This is part of the build.gradle file.
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "<meu.package>"
minSdkVersion 17
targetSdkVersion 26
It seems to have to do with this version, because in other applications the creation of similar folders usually occurs on the same machine.
Incidentally, when I install the other applications Android creates the folder emulated / 0 / Android / data / automatically and in this application this does not happen.