0

I am adding support for importing a specific type of file format from another (Windows) app. This particular format keeps the data in a pair of files. These files use the same filename, with different extensions, eg myfile.ext and myfile.ex2.

The scenario is as follows:

  1. The user selects myfile.ext from dropbox, google drive or other
  2. By knowing the filename/path, I want to resolve myfile.ex2 at the same location and open it

The problem is, that when using the URI provided from the file chooser, the URI's looks like this:

content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D154

There are several solutions here involving getting the real file name using getContentResolver(), and pick the original file name from the returned cursor.

Is there a generic way to obtain an absolute path with the actual file name, change extension and then open it ?

Oyvind
  • 568
  • 2
  • 6
  • 22

1 Answers1

0

There are several solutions here involving getting the real file name using getContentResolver(), and pick the original file name from the returned cursor.

None of which will work reliably, particularly with the providers that you cited.

Is there a generic way to obtain an absolute path with the actual file name, change extension and then open it ?

No.

First, there is no absolute path, because the files in question do not have to be on the device. After all, "dropbox, google drive or other" are cloud services, at least for many values of "other". There is no requirement that just because a cloud service downloaded one file to the device, that is has to download all files to the device.

Second, there is no absolute path that you can necessarily use, because an intelligently-written "dropbox, google drive or other" will have the files on the app's portion of internal storage, which your app cannot access.

Third, a Uri is an opaque handle, just as a URL is. There is no requirement that the Web sites of "dropbox, google drive or other" provide Web URL relative addressing between two arbitrary files held by their services. Similarly, there is no requirement that the on-device file providers provide Uri relative addressing between two arbitrary files held by their services, or even to use filenames that are recognizable.

The main options that I see for you are:

  1. Have the user pick each file individually.

  2. Have the user combine the files, such as putting them in a ZIP archive.

  3. Have the user pick each file, but in one Storage Access Framework ACTION_OPEN_DOCUMENT operation, via EXTRA_ALLOW_MULTIPLE (warning: only practical if your minSdkVersion is 19 or higher, and you will have to handle the case where the user screws up and does not choose exactly two files).

  4. Have the user organize the files, such as putting them in a single directory/folder, and use the Storage Access Framework's ACTION_OPEN_DOCUMENT_TREE Intent (warning: only practical if your minSdkVersion is 21 or higher).

  5. Switch to using direct Web service APIs for whatever service(s) you wish to integrate with, if they, through their APIs, offer something more amenable to you.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491