Renan Willamy
Renan Willamy

Reputation: 173

Can't create a directory on /storage/emulated/0 on emulator

I'm trying to create a directory on my android emulator but I can't do it, I already have the permission on manifest write_storage, and I get no erros but mkdir() return false, I verify if external storage is writtable too and it is, it works on physical devices my code:

 /// Cria uma nova pasta para colocar o backup
    File direct = new File(Environment.getExternalStorageDirectory(),
            "/Financas RW Backup");
    try {


        if (!direct.exists()) {
            if(isExternalStorageWritable()&&isExternalStorageReadable()) {
              if(  direct.mkdir()) {
                  fachada.showMessage(ExportImportDB.this," Criado");
              }else{
                  fachada.showMessage(ExportImportDB.this," Não Criado");
              }
            }
        }
    } catch (Exception e) {
        fachada.showMessage(this, e.toString());
    }

Upvotes: 14

Views: 22804

Answers (3)

Bibaswann Bandyopadhyay
Bibaswann Bandyopadhyay

Reputation: 3547

I had the same problem with Android 10. I solved it by adding android:requestLegacyExternalStorage="true" to my <application> element in the manifest

Note: We can also use context.getExternalFilesDir(Environment.DIRECTORY_PICTURES). There are other directory type constants in Environment like DIRECTORY_MUSIC, DIRECTORY_MOVIES etc.

Upvotes: 5

Shane Murphy
Shane Murphy

Reputation: 389

I had a similar issue was fixed by using mkdirs instead of mkdir

Upvotes: -1

Gueorgui Obregon
Gueorgui Obregon

Reputation: 5087

You have to options:

  • Downgrade your compileSdkVersion and targetSdkVersion to lower version in your build.gradle
  • Request write permission at runtime is required on Android 6 .

I personally don't advice the first one. For more info check:

Hope this helps!!

Upvotes: 11

Related Questions