Writing to external SD card in Android 5.0 and above

This, right here.

I’m going to chime in here. I’m able to fully sync (read & write) folders on my external SD card in Android 7.0 without root and WITHOUT formatting my SD card as adoptable storage (I kept it in it’s removable exfat format). Tested on my Moto X Pure running stock Android 7.0 and on my LG-G3 running Lineage 14.1 Android 7.1.2. In the Android app under Settings/Behavior there is an option to use the Advanced Folder Picker, check this off. My external sd card is labeled /storage/0123-4567 on my device. This number will vary depending on your phone. In this particular setup I’ll explain how I sync my entire 90gb music library to my external 128gb sd card on Stock Android 7.0, MotoXPure:

1: Add your computer to Android by scanning it’s ID. Wait for it to add, and don’t select any folders to share with it.

2: On your Android, create the folder you want to sync with and create a .stfolder inside the folder (or just add a .stfolder to an existing folder if you already have the folder created the device).

3: On the computer create a new folder share within Syncthing’s GUI. In my case it was my Music folder on my PC. Select the device to share the folder with (your Android) and choose all of your preferences. I turn all file versioning off for my needs and on my PC I was sure to set it as “Send Only” so that I can’t screw up and delete something from my PC’s master music folder.

4: Wait for the notification to appear on your Android, then click it when it arrives. Name the sync to whatever you want and then in the “Path” area select your external sd (which will be labeled something like /storage/0000-0000). Then once the SD is selected, navigate to the folder on the external sd that you’ll be syncing (in my case it’s /storage/0123-4567/Music).

5: Now wait for the devices to re-communicate with each other and syncing will begin. You may need to restart Syncthing on both devices to get it to initiate.

6: That’s it…both folders are now syncing. Any music file I add from my PC will write to my phone’s external SD if Syncthing is running. Even though I am rooted, root is not required.

Note: Both of my folders (music on PC and music on Android’s external sd) were both already on each device. After setting up the sync, the initial scan took quite a while to complete (about 40min), but after that it’s working properly. All changes are synced immediately based on your selected scan interval. I took one album and changed the tags & embedded a cover image into each mp3 file and they were synced to my Android almost immediately. Can’t be happier, this is working great.

1 Like

That is not supposed to work. Maybe LineageOS has a change that allows this, or you installed some app or script that allows it.

1 Like

The only downside is that the computer shows that it’s still syncing (hangs at 95%) and the phone says it’s out of sync. But it’s not, all files/changes are synced correctly. I don’t know, seems to just work.

A new (I think) proposal for using the Android SAF from go without having to do any cross-language calls. This is going to be a little odd, but as far as I can tell it will work. The ingredients in this insane plan are:

  1. UNIX domain sockets, to which both Android Java and Go have access (I think), and, in particular, the ability to pass file descriptors across UNIX domain sockets.

  2. Some code in the syncthing-android service which knows how to drive the SAF in response to commands received across a socket connection.

  3. Some code in syncthing itself to provide a synthetic mountpoint; a filesystem driver in parallel to basicfs which knows how to translate requests for files below the mountpoint into commands on the socket.

Does this seem like a reasonable plan? I’d be happy to start prototyping if so.

I think you should not. Instead, add your mind to New Android app "Syncthing Lite"

So this should work in theory if you can pull it off, but I couldn’t find the right syscalls in java exposed to be able to send descriptors around.

From a quick glance, it seems that Syncthing Lite, while interesting, is lacking a scanner and so is further away from my goal of “sync to SD card” than some mucking about with file descriptors and sockets in syncthing / syncthing-android? Could you expand on your comment?

I fished around a little more and, yeah, it looks like they’re not plumbed up into Java, but people have had success using JNI bindings on Android for the requisite sendmsg() call. See, for example, https://github.com/PeterCxy/SocksDroid/blob/d572359ac55757e7956ff4806fea3fda70e8887f/app/src/main/jni/system.cpp . Golang has them plumbed through, too; see /src/syscall/syscall_unix_test.go 's TestUnixRightsRoundtrip. (The one link per message thing is a drag, though I understand why.)

Yeah, that will most likely work, but I don’t expect it to be easy, as you have to devise an RPC for creating files as well as opening existing ones, make sure the pipe is always there, etc, which might require a separate service. How do you start that service from go, etc.

The idea is that a native java implementation of syncthing is potentially a better place to spend the effort.

I have never used unix sockets myself, so I’m not sure if those would work. Maybe an HTTP api could also be used instead?

And yes, syncthing-lite is only meant to be a client, without any functionality for synchronization. It might be possible to add that, but it would probably involve a lot of extra effort.

No, you pass file handles between processes you need to use special syscalls via a unix socket (or potentially a pipe).

not sure where everyone is at with this, but the on version 0.9.11 (4121) on Android I was able to get syncthing to sync one way to the external SD card on my LGV20 running 7.0 Nougat while running as root. the only problem was I couldn’t do it consistently in that after enabling root and restarting syncthing, I was able to get a complete sync once. after exiting syncthing any attempts to start it up again resulted in a failure to read the config. I’ve tried changing the ownership of all the files in /data/data/com.nutomic.syncthingadroid, but no dice leaving the only way to restore everything from a TitaniumBackup. even copying this folder’s contents elsewhere and restoring it after would not work for some reason. now on version 0.10.2 and all the versions in between, I can enable/disable root entering and exiting syncthing with no problems, however with root enabled it gets stuck at the “Loading” stage in the beginning never pulling up the list of directories I’ve set for sync. anyone have any suggestions?

@dimmok yes, root option is attributed experimental, because nobody found time to get it working properly yet.

Since you already rooted your device, you might have some success with ExtSD fix. v1.9b worked well for a couple of LineageOS installations here lately.

Is it able to access external card via Android NDK ?

It should be a simple task to implement go wrapper over C++ (NDK) code. I think porting syncthing to android was much more harder to do.

The issue talks about accessing internal storage which we don’t have problems accessing. It states that you only needs permissions to access external storage, which was perhaps true in 2012, but is no longer true.

Will not insist, as I’m not Android programmer, seems this restriction is done in Dalik/ARM virtual machine, not by Linux kernel/SELinux. At least, I can do “shell@E5333:/storage/sdcard1 $ echo > a” from shell. I belive, utilities like “echo” and even Dalvi/ARM are build with Adnoid NDK, and they have write ablity to external sd. It’s just an opinion, sorry if I’m wrong.

Thanks, it works! And I also found out the updates on the manifest file will only take effect after you re-install the app. 2015-06-16

Well, compile a Go binary and see if you can get a go binary to do that without root.

As with all things Android I expect there is variation between ROMs. Maybe yours doesn’t enforce this. I’m pretty sure other variants do, or this issue would have been a non issue. There’s also no indication the comment you quote relates to 5.0+. The rest of the post predates 5.0 by years.