Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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());
    }
like image 379
Renan Willamy Avatar asked Mar 18 '16 12:03

Renan Willamy


1 Answers

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.

like image 75
Bibaswann Bandyopadhyay Avatar answered Oct 01 '22 02:10

Bibaswann Bandyopadhyay