0

I am trying to dump some output for an app onto the SD such that it can be accessed by the user via usb. Simple enough, right? So, this is what I tried.

File sdCard = Environment.getExternalStorageDirectory();
File dirExt = new File (sdCard.getAbsolutePath() + "/dir1/dir2");
dirExt.mkdirs();
File fileExt = new File(dirExt, "filename");

Log.d("FILE", "Is directory: "+sdCard.isDirectory()+". Exists: "+sdCard.exists()
        +". Can read: "+sdCard.canRead());

Log.d("FILE", "Is directory: "+dirExt.isDirectory()+". Exists: "+dirExt.exists()
        +". Can read: "+dirExt.canRead());

Log.d("FILE", "Is file: "+fileExt.isFile()+". Exists: "+fileExt.exists()
        +". Can read: "+fileExt.canRead());

String string = "Hello!";

try{
    FileOutputStream fos = new FileOutputStream(fileExt);
    fos.write(string.getBytes());
    fos.close();
}catch (Exception e){
    Log.e("FILE", "catch", e);
}

I also included the write permission in the manifest file and checked the sd state from environment, it said "mounted". So, we should be good to go. However, somehow I don't have access to anything on the sd directory, as explained by the logs:

4085-4085/com.my.class D/FILE: Is directory: true. Exists: true. Can read: false
4085-4085/com.my.class D/FILE: Is directory: false. Exists: false. Can read: false
4085-4085/com.my.class D/FILE: Is file: false. Exists: false. Can read: false

Is it possible to write anything on the sd-card at all? I am using an emulator with 200 MB SD storage.

lch
  • 2,028
  • 2
  • 25
  • 46
hasdrubal
  • 1,024
  • 14
  • 30
  • Possible duplicate of [Find an external SD card location](http://stackoverflow.com/questions/5694933/find-an-external-sd-card-location) – Adam Apr 30 '17 at 14:29
  • https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it https://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl – CommonsWare Apr 30 '17 at 14:43
  • `dirExt.mkdirs();` Only call mkdirs if the directory does not exist. And return if it returns false when it could not create the directory. And that path is not of the SD card. – greenapps Apr 30 '17 at 15:35
  • Please state your device/emulator's API level (i.e. 23 for Marshmallow, 21/22 for Lollipop, etc.). Also your question is missing the LogCat. – Sufian Apr 30 '17 at 18:26

0 Answers0