1

The Samsung Device is a T810 with Android 7.0 API 24 and has a SD Card that other apps we have created have written to the SQLite DB on the SD Card with no issues. Sad to say we are not sure if this was before the Android 7.0 update. As of now we can not use code posted below to write to the SD Card. On the Emulator running API 26 the device file explorer is showing this information Database Emulator /mnt/user/0/primary/Android/data/com.androidstackoverflow.onepass/files/Documents/PassWord Storage /storage/emulated/0/Android/data/com.androidstackoverflow.onepass/files/Documents/PassWord SD Card Emulator /sdcard/Android/data/com.androidstackoverflow.onepass/files/Documents/PassWord

    public String getThePath(){

    File removable = ContextCompat.getExternalFilesDirs(this,null)[0];
    if (removable.exists() && removable.canRead() && removable.canWrite()) {
        THE_PATH = String.valueOf(removable);
        THE_PATH = THE_PATH + "/Documents/";
    }else {
        Toast.makeText(getApplicationContext(),"NO SD CARD", Toast.LENGTH_LONG).show();
    }
    return THE_PATH;
}

The question we would like help with is this a Samsung device issue or is the emulator not truthful information? Permissions are set both READ and WRITE We have even tried to hard code the path. Looked at numerous SO posts on this subject. The folder Documents/DB Name gets created with no issues

James_Duh
  • 1,321
  • 11
  • 31
  • "As of now we can not use code posted below to write to the SD Card" -- first, that code is not writing anything. Second, the first element in the `getExternalFilesDirs()` array will point to [external storage](https://commonsware.com/blog/2017/11/14/storage-situation-external-storage.html), not [removable storage](https://commonsware.com/blog/2017/11/15/storage-situation-removable-storage.html). So... what is your code and what are your symptoms that demonstrate that you cannot write to removable storage? – CommonsWare Apr 12 '18 at 19:22
  • @CommonsWare I understand the difference removable vs external and have tried to use [1] but array error shows up I would swear at one time the [1] worked So where do we go from here ? – James_Duh Apr 12 '18 at 19:26
  • @CommonsWare looked at this so many time https://stackoverflow.com/questions/40068984/universal-way-to-write-to-external-sd-card-on-android/40201333#40201333 – James_Duh Apr 12 '18 at 19:28
  • "have tried to use [1] but array error shows up" -- then the Android SDK doesn't think that the device has a removable storage volume, apparently. – CommonsWare Apr 12 '18 at 19:28
  • @CommonsWare I have not tried to use [1] code on real device so is the emulator not think it has a SD Card – James_Duh Apr 12 '18 at 19:30
  • I am not aware that the Android SDK emulator has an option to emulate removable storage. AFAIK, you need to test your removable storage logic on hardware. Personally, I do not recommend working with SQLite directly on removable storage, as bad things can happen if that storage is removed while database I/O is going on. IMHO, work with internal storage for database I/O, with "export" or "backup" or similar options to copy the (closed) database to removable storage. – CommonsWare Apr 12 '18 at 19:34
  • @CommonsWare Testing with[1] on real device now I have a back up routine but only testing on emulator It means a lot that you jumped in I know who I am chatting with – James_Duh Apr 12 '18 at 19:37
  • @CommonsWare Works on real device with [1] used in the code any ides how to use this on the emulator Does New Edition of Book cover this – James_Duh Apr 12 '18 at 19:40
  • Well, I still do not know what your symptoms are. As I commented originally, this code does not write anywhere. If you are seeing your `Toast`, which of the three pieces of your `if` is the one (or ones) returning `false`? If you are having some other symptoms, what are they, and what is the code that is causing problems? BTW, please use `File` objects over `String`, and use the two-parameter `File` constructor (`new File(removable, "Documents")`) instead of string concatenation, for safety's sake. – CommonsWare Apr 12 '18 at 19:42
  • @CommonsWare Only issue is on emulator with [1] As for the code not writing on the emulator it writes to the DB and on the device it writes to the DB but only in Internal Storage YES thanks for the tip syntax on file creation BOOK QUESTION ? ? – James_Duh Apr 12 '18 at 19:47
  • "Only issue is on emulator with [1]" -- even on hardware, you cannot assume that there is removable storage, as not all devices offer it. You cannot assume that there is exactly one removable storage location, either, as there could be two or more. So, you need to have a plan for handling 0, 1, or N removable storage locations. Part of that will be looking at the length of the array that you get back from `getExternalFilesDirs()`. – CommonsWare Apr 12 '18 at 19:49
  • @CommonsWare So if File removable size = 0 force storage to Internal Not sure how to check the Array I assume like any other array Thanks I will not take any more of your time This means a lot to me – James_Duh Apr 12 '18 at 19:53
  • "I assume like any other array" -- yes, sorry, I should have been more plain. – CommonsWare Apr 12 '18 at 19:55
  • @CommonsWare I will post an answer to this question only because the subject on SO is so confusing with the OLD post I know you are not bounty hunting points Any One Reading This Dialog the answers are here if you are trying to write to your SD Card moral DO NOT trust the emulator for testing – James_Duh Apr 12 '18 at 20:05

1 Answers1

0

First the information to produce this answer was provided by @CommonsWare It is posted here so others can learn not so I can grab points The issue was how to manage the storage location on a Samsung Tablet It is most important that you understand testing if the SD Card is mounted on the Emulator is not going to work Below is my very odd code to test if the SD Card was mounted or not. Then the revised code that lets you manage the storage location

    File fi = new File("storage/");
    File[] lst = fi.listFiles();//
    String top = String.valueOf(lst[1]);
    String bot = String.valueOf(lst[0]);

    if(bot.contains("-")){
        STORAGE_LOCATION = 1;
    }
    if(top.contains("storage/enc_emulated")){
        STORAGE_LOCATION = 0;
    }
    public String getThePath(){

    File removable = ContextCompat.getExternalFilesDirs(this,null) 
   [STORAGE_LOCATION];

    if(STORAGE_LOCATION == 1){
        THE_PATH = String.valueOf(removable);
        THE_PATH = THE_PATH + "/Documents/";
    }
    if(STORAGE_LOCATION == 0){
        THE_PATH = String.valueOf(removable);
        THE_PATH = THE_PATH + "/INTERNAL/";
        Toast.makeText(getApplicationContext(),"NO SD CARD", 
    Toast.LENGTH_LONG).show();
    }
        return THE_PATH;
}
James_Duh
  • 1,321
  • 11
  • 31