Search code examples
androidandroid-contentprovidershared-directory

Android - How to list files inside a folder with a content provider


I'm developing an application to read files through a content provider defined in another app (say app B). Now, I'm able to read a specific file from my app (app A) using:

[...]
InputStream content = getContentResolver().openInputStream(content://.../path_of_the_folder/file_name.txt);
BufferedReader reader1 = new BufferedReader(new InputStreamReader(content));
String linetext;
       while ((linetext = reader1.readLine()) != null) { //printout
[...]

but how can I list all files stored in the folder it self? I'm trying to use the "list" method defined in the class File, but how can I convert what I get from openInputStream to a File object?

Thank you very much all


Solution

  • The ContentProvider basic protocol does not support browsing of directory structures this way.

    If you happen to be the author of both apps, you are welcome to invent your own mechanism for this, such as:

    • use query() on some modified version of your folder's Uri to have it return a Cursor representing the folder's contents, or

    • use call() and have it return a Bundle that contains a list of child folder Uri values and a list of child document Uri values, or

    • stream back some JSON or XML or something as the "content" of a folder, where the JSON or XML describes the contents of the folder