1

After years of developing my directory manager, I finally found how to modify sdcard files. This is by using Storage Access Framework, api level >= 21.

Problem is, DocumentsContract is highly undocumented.

I am using Android 7.0. and noticed that DocumentsContract.Document.COLUMN_DOCUMENT_ID is is a partial file system path

id = F6F5-1B1D:Download/download
filepath = /storage/F6F5-1BD/Download/download

This works in my benefit as instead of recursively query children Uri to match a filesystem path, I can just convert the document uri to its file system path.

My problem now is that MAYBE not all Android 7 devices will return this kind of Uri structure. I think I read that Android kitkat document id is an integer instead. Meanwhile Android 8 document id is same as the file system path.

7
  • 1
    Yes, saf uries can mostly be converted to a file system path. That is interesting. But what more? For what would you use such a path? You can do all with saf. On removable media using a path you only have read access. It brings nothing for me. Why do you want a path?
    – blackapps
    Feb 16, 2021 at 23:48
  • @blackapps good question. I am trying to implement Internal Storage with java.io.File and SdCard with SAF. In general I dont completely understand SAF, so I need to better understand it and see if I can simply use it for everything. Feb 17, 2021 at 2:22
  • You should make a file explorer for all partitions. Primary, secundary (micro SD card) and usb otg. AND you should make a saf explorer for the same partitions.
    – blackapps
    Feb 17, 2021 at 10:58
  • @blackapps you asked why I needed a java.io.File. Well I have been going through my code, trying implement SAF. java.io.File is well baked in! Anyhow, I have a file path branch view that allows users to click to any branch of the file path. This surely requires me to know the filepath instead of some opaque ID. Or come to think of it, I could just have a list of branch id(s) and assign each as a tag to my branch view. Then when I click a branch, I use the ID handle to open that branch path. Feb 17, 2021 at 23:01
  • It is unclear to me what you are doing. With ACTION_OPEN_DOCUMENT_TREE you can let the user choose all partitions. Having a tree uri you can list all subdirs an files in it. Recursively. What do you need more? No need for paths.
    – blackapps
    Feb 17, 2021 at 23:16

1 Answer 1

2

I am using Android 7.0. and noticed that DocumentsContract.Document.COLUMN_DOCUMENT_ID is is a partial file system path

For that particular device, for that particular document provider, for that particular document, that may be the case.

My problem now is that MAYBE not all Android 7 devices will return this kind of Uri structure

It is not strictly tied to a OS version. For example, all Android 7 devices will return other structures for other document providers, such as Google Drive.

Beyond that, even for the external storage provider, this sort of behavior can be changed by:

  • Google, in either OS versions or updates to system components via the Play Store
  • Device manufacturers, if they supply their own implementation of this document provider
  • Users, if they have installed custom firmware

Treat identifiers as opaque values. Do not make assumptions based on apparent patterns in their values.

5
  • Thanks for your answer. It wasn't an assumption but an hypothesis. But you clearly state, treat id as opaque values. May I ask if you read that in the Android standards? Feb 17, 2021 at 2:29
  • @user13947194: "May I ask if you read that in the Android standards?" -- if by "that", you mean the idea of treating IDs as opaque values, not when I wrote it. I have been programming computers for over 35 years, and you learn lessons along the way. Now that I happen to look at the documentation, though, it has "Unique ID of a document. This ID is both provided by and interpreted by a DocumentsProvider, and should be treated as an opaque value by client applications". Feb 17, 2021 at 11:54
  • I finally implemented browsing with SAF. My problem now is opening files. Most 3rd party Activities dont support document uri. Thus I need to convert to content uri. But how? Note: If I am able to convert to content uri, I would at the same time be able to get the real file path. Feb 19, 2021 at 22:15
  • @user13947194: "Most 3rd party Activities dont support document uri" -- that would be rather surprising, since it is been required for working with Gmail for years. Feb 19, 2021 at 22:18
  • Try opening a file with intent ACTION_VIEW Feb 21, 2021 at 2:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.