How does Total Commander write to Ext. SD card in Android?

Support for Android version of Total Commander

Moderators: white, Hacker, petermad, Stefan2

Post Reply
matrix.wizard
Junior Member
Junior Member
Posts: 5
Joined: 2018-05-16, 20:05 UTC

How does Total Commander write to Ext. SD card in Android?

Post by *matrix.wizard »

I'd first like to say thanks for one of the most useful programs ever! Total Commander is the best.

I have a Xiaomi phone with Android OS 7.0 (Nougat) and it seems Total Commander is able to write to the External SD card when I attempt to copy a file onto it. However what happens is it warns first that on Android 5 (and above) the external SD card is write protected and you need to choose a directory from a special Android dialog to give T.C. write rights. and a bit more.
But if I hit cancel it copies it anyway to the folder I selected on the external SD card. The phone is on a stock ROM and is rooted but currently has no patches implemented for the external SD write fix.
What I wonder is how it does this and if it is possible this method might be a way to create a permanent external SD card write fix. So far the methods I have tried - various patches (on this rooted phone) only ended up creating some serious problems - Google Services quit working and numerous force closes on some apps. I ended having to reinstall the stock ROM. While it's nice T.C. seems to bypass this block when I hit cancel I would like to understand how it does this. Thank you for any help with this!
matrix.wizard
Junior Member
Junior Member
Posts: 5
Joined: 2018-05-16, 20:05 UTC

Post by *matrix.wizard »

Just to add a little more info hitting the cancel buttons seems to do the same exact thing as following the rest of the directions in the popup dialog warning about the external card write protection. Instead of going the choose the external SD card and hitting select at the bottom I seem to be able to avoid that operation by just hitting cancel.
matrix.wizard
Junior Member
Junior Member
Posts: 5
Joined: 2018-05-16, 20:05 UTC

Post by *matrix.wizard »

One more note of a discovery I just made. Since this phone is rooted I was able to convert Total Commander to a System app. Once that was done it copies files from the internal to the external SD card without any popups or warnings at all. That's great news for anyone with a rooted phone.
User avatar
Hacker
Moderator
Moderator
Posts: 13052
Joined: 2003-02-06, 14:56 UTC
Location: Bratislava, Slovakia

Post by *Hacker »

matrix.wizard,
Are we talking about TC for Windows or TC for Android?

Roman
Mal angenommen, du drückst Strg+F, wählst die FTP-Verbindung (mit gespeichertem Passwort), klickst aber nicht auf Verbinden, sondern fällst tot um.
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 48021
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Post by *ghisler(Author) »

TC first tries to write to the card with the new documents api functions. For this it has to request write access via the warning dialog. If this fails, it tries with root rights.
Author of Total Commander
https://www.ghisler.com
matrix.wizard
Junior Member
Junior Member
Posts: 5
Joined: 2018-05-16, 20:05 UTC

Post by *matrix.wizard »

Hacker wrote:matrix.wizard,
Are we talking about TC for Windows or TC for Android?

Roman
As seen in my first post this is about use on Android.
matrix.wizard
Junior Member
Junior Member
Posts: 5
Joined: 2018-05-16, 20:05 UTC

Post by *matrix.wizard »

ghisler(Author) wrote:TC first tries to write to the card with the new documents api functions. For this it has to request write access via the warning dialog. If this fails, it tries with root rights.
Thank you sir! I've been using it a couple weeks now as a system app and it works great. No popup dialog even - it just copies without hesitation.
nixomose
Junior Member
Junior Member
Posts: 2
Joined: 2018-12-27, 22:07 UTC

Re: How does Total Commander write to Ext. SD card in Android?

Post by *nixomose »

TC first tries to write to the card with the new documents api functions. For this it has to request write access via the warning dialog. If this fails, it tries with root rights.
Can you expand on that?
I have the same problem as you and everybody else, except you seem to have solved it.
I can read from the external sd card, but can't write to it. I have a file sync program I wrote so I need read and write random access to individual files.
But all I get from the 'select a directory' dialog is a url. How do you map the url to the /storage/<uuid> path.
I've seen a bunch of people try to no reliable avail.
And then there's the whole random access problem.
Any suggestions or tips? Thanks.
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 48021
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: How does Total Commander write to Ext. SD card in Android?

Post by *ghisler(Author) »

There isn't any function on Android to map between /storage/ and content provider URLs.

For devices with API 24 or newer, you can use the following approach:
1. Get the drive UUID from the path. It's simply the first subfolder after /storage, e.g. 1234-5678
2. Call StorageManager - getStorageVolumes
3. For each volume, call getUuid
4. If the UUID matches the one in /storage/1234-5678, we have our volume!
5. Call storageVolume - createAccessIntent
6. Invoke that intent to get permission from the user
7. We get a callback with a content URI for the base of that volume, so we can now write to it via Documents API

For older APIs (e.g. Android 5), use the following:
1. Get the drive UUID from the path. It's simply the first subfolder after /storage, e.g. 1234-5678
2. Tell the user that he needs to go to the root of the medium and click on 'Select' at the bottom
3. Call intent: intent=new Intent("android.intent.action.OPEN_DOCUMENT_TREE");
intent.setFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
4. We get a callback with a content URI for the base of that volume, so we can now write to it via Documents API.
Verify that we got the right location by looking for the UUID in the URI

Now that you have the content URL for the target, call contentResolver.takePersistableUriPermission(dataUri, takeFlags); with
int takeFlags = intent.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
to keep the access right accross sessions, and store the content URI for that drive, so you don't need to request access again.

Doing the actual writing could be done via support library (I haven't used this approach) or via Documents Api directly.
Author of Total Commander
https://www.ghisler.com
nixomose
Junior Member
Junior Member
Posts: 2
Joined: 2018-12-27, 22:07 UTC

Re: How does Total Commander write to Ext. SD card in Android?

Post by *nixomose »

very slick, thanks for the tips. happy new year.
Post Reply