-1

I want to back up SQLite db to my sd card on Oppo A37 I have permissions to write to external in manifest

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

and this line of code

String outFileName = Environment.getExternalStorageDirectory().getPath() + 
"/SQLite/" + DBNAME + "_" + ext;

After running db file will show up in several incarnations:

/storage/emulated/0/Sqlite/
/mnt/sdcard/SQLite/    
/sdcard/SQLite
/storage/sdcard0/SQLite/

Maybe more, probably many alias for same thing but all in phone internal storage.

I discovered that what I want is "/storage/sdcard1/SQLite/" so I tried with adb

 adb -d shell "run-as com.photobangkok.expences cat 
/data/data/com.photobangkok.expences/databases/money_DB > /storage/sd
card1/SQLite/money_DB"

It does work but

String outFileName = "/storage/sdcard1/SQLite/" + DBNAME + "_" + ext;

produces an error (Permission Denied) and is frown upon by LINT

Running out of ideas of how to use my sd card! Can someone help me telling me the proper line of code to use ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
alberto
  • 115
  • 4
  • 19
  • 1
    Possible duplicate of [Android write to sd card folder](https://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder) – Ashish Detroja Nov 02 '17 at 07:06
  • the answers suggest to make a Dir in getExternalStorageDirectory() which returns emulated/0 or the shareable space which is in internal phone space – alberto Nov 02 '17 at 07:16
  • For external : Possible duplicate of https://stackoverflow.com/questions/44636323/creating-folders-and-writing-files-to-external-storage – Ashish Detroja Nov 02 '17 at 07:17
  • Possible duplicate of https://stackoverflow.com/questions/40068984/universal-way-to-write-to-external-sd-card-on-android – Ashish Detroja Nov 02 '17 at 07:18
  • it's quite hard to write to external SDCard storage in Android. Basically only way is by using SAF framework. – Vladyslav Matviienko Nov 02 '17 at 08:20

1 Answers1

0

Once you discovered the path to the SD card you will discover that you cannot write to it anymore. Google does not want that. Often you can write though to one specific app directory like ..../Android/data/<packagename>/files.

The last path you can find -if you are lucky- with getExternalFilesDirs(). Often its the second entry.

If you want to write on the whole SD card you should use the Storage Access Framework and start with Intent.ACTION_OPEN_DOCUMENT_TREE.

greenapps
  • 11,154
  • 2
  • 16
  • 19