How does Total Commander write to Ext. SD card in Android?
Moderators: Hacker, petermad, Stefan2, white
-
- Junior Member
- Posts: 5
- Joined: 2018-05-16, 20:05 UTC
How does Total Commander write to Ext. SD card in Android?
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!
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!
-
- Junior Member
- Posts: 5
- Joined: 2018-05-16, 20:05 UTC
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.
-
- Junior Member
- Posts: 5
- Joined: 2018-05-16, 20:05 UTC
- ghisler(Author)
- Site Admin
- Posts: 50532
- Joined: 2003-02-04, 09:46 UTC
- Location: Switzerland
- Contact:
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
https://www.ghisler.com
-
- Junior Member
- Posts: 5
- Joined: 2018-05-16, 20:05 UTC
-
- Junior Member
- Posts: 5
- Joined: 2018-05-16, 20:05 UTC
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.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.
Re: How does Total Commander write to Ext. SD card in Android?
Can you expand on that?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.
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.
- ghisler(Author)
- Site Admin
- Posts: 50532
- Joined: 2003-02-04, 09:46 UTC
- Location: Switzerland
- Contact:
Re: How does Total Commander write to Ext. SD card in Android?
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.
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
https://www.ghisler.com
Re: How does Total Commander write to Ext. SD card in Android?
very slick, thanks for the tips. happy new year.