0
I’m trying to create a folder on Android to save the photos of the vouchers, but I’m not succeeding. I’ve given 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 rules of Android 7.0, I am requesting these permissions at runtime.
The code goes 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 stream goes straight to the Creatifolder routine because this permission has already been requested in 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 Create routine the message saying that the folder cannot be created occurs every time.
This is part of the build.Radle 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 my creation of similar folders usually occurs on the same device.
By the way, when I install the other apps Android creates the folder Emulated/0/Android/data/ automatically and in this application it does not happen.
The only reason I can’t find is that the directory is not accessible. You can check using getExternal(). Anyway, from version 19 (Android 4.4) should not use this method. When dealing with the External Storage keep in mind what is said in this reply.
– ramaral