0

I want to open a particular directory (in a file explorer app) where images saved by my app are stored. I can get the Uri of that directory by Uri.parse(imagesDir.getAbsolutePath()). I tried this, this and others but it just does nothing. This is how my code looks as of now:

Uri selectedUri = Uri.parse(imagesDir.getAbsolutePath());
int OPEN_REQUEST = 1337;
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setDataAndType(selectedUri, DocumentsContract.Document.MIME_TYPE_DIR);
if (intent.resolveActivityInfo(getPackageManager(), 0) != null) {
    startActivityForResult(intent, OPEN_REQUEST);
} else {
    Log.e("MainActivity", "Could not launch intent");
}

P.S: The value of imagesDir.getAbsolutePath() = /storage/emulated/0/Draw Easy

Shreemaan Abhishek
  • 1,134
  • 1
  • 7
  • 33

2 Answers2

1

I want to open a particular directory where images saved by my app are stored

Android has never really supported this.

I can get the Uri of that directory by Uri.parse(imagesDir.getAbsolutePath()).

That is an invalid Uri. At best, use Uri.fromFile(imagesDir).

This is how my code looks as of now

ACTION_OPEN_DOCUMENT does not take a Uri in the "data" facet.

The closest thing to what you want is to add EXTRA_INITIAL_URI to the Intent. However, this is only documented to work with a Uri that you previously obtained from ACTION_OPEN_DOCUMENT or ACTION_OPEN_DOCUMENT_TREE. It is unlikely to work with a Uri from some place else, such as from some File.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Okay, so my app does some kind of photo manipulation and then saves it in a directory. When the user presses the save button. If the image was saved successfully I want a SnackBar containing an action to appear. When the button is clicked I want to navigate to the directory where the image was saved. – Shreemaan Abhishek Sep 05 '20 at 13:40
  • Hope this makes sense of what I am actually trying to do. Thanks for responding android king!! – Shreemaan Abhishek Sep 05 '20 at 13:41
  • @AbhishekChoudhary: "When the button is clicked I want to navigate to the directory where the image was saved" -- to what end? What are you expecting the user to do at this point? `ACTION_OPEN_DOCUMENT` is the equivalent of a "file open" dialog in a desktop OS. What are you asking the user to open? – CommonsWare Sep 05 '20 at 14:25
  • I used that code snippet from a post in SO expecting for it to work. Honestly I don't even know what that snippet actually does. " -- to what end? What are you expecting the user to do at this point?" I want the user to know the location of the directory where the image was saved. – Shreemaan Abhishek Sep 05 '20 at 14:35
  • @AbhishekChoudhary: " I want the user to know the location of the directory where the image was saved" -- then display the path in a `TextView` or something. Or, rather than *you* deciding the location for saving the image, use `ACTION_OPEN_DOCUMENT` or `ACTION_OPEN_DOCUMENT_TREE` originally, so that the *user* decides where the *user* wants the *user's* image to be placed on the *user's* device or in the *user's* chosen cloud storage locations. Then, you do not need to tell the user the location in the end, as the user told you the location in the first place. – CommonsWare Sep 05 '20 at 14:38
  • I want that directory to be opened in a file explorer – Shreemaan Abhishek Sep 05 '20 at 15:03
  • @AbhishekChoudhary: There is no general-purpose file explorer on Android, let alone a standard protocol for interacting with one. `ACTION_OPEN_DOCUMENT`, again, is the equivalent of a file-open dialog. The user cannot do anything there, except choose some document for you to open. – CommonsWare Sep 05 '20 at 15:08
  • I am sorry that you had to say the same thing time and again, I understood that what you wanted to say the very first time Sir! The feature I want to implement is equivalent to "Show in Explorer" in Windows – Shreemaan Abhishek Sep 05 '20 at 15:13
  • @AbhishekChoudhary: Sorry, but the equivalent of Explorer does not exist in Android. Whether any given device ships with a file explorer is up to the device manufacturer, and there is no standard `Intent` structure for "show this filesystem directory in a file explorer". What you are seeking is not unreasonable; it just does not really exist. – CommonsWare Sep 05 '20 at 15:22
  • In xiaomi devices (using the preinstalled gallery app) you can click on the filepath of an image and then it opens up the location of that image in the file manager. So I thought I could achieve something like that – Shreemaan Abhishek Sep 05 '20 at 15:31
  • @AbhishekChoudhary: Xiaomi's gallery app and Xiaomi's file manager app are both created by Xiaomi and can do whatever Xiaomi wants. You can examine Logcat to see what sort of `Intent` structure is used by the gallery app, and you can try to mimic it. However, that is not guaranteed to work even across all *Xiaomi* devices, let alone all ~2.5 billion Android devices. – CommonsWare Sep 05 '20 at 15:59
0

SAF is pain. Uri.fromFile(file.getAbsolutePath()) doesn't work because returns uri likes file://sdcard.... But DocumentsContract.EXTRA_INITIAL_URI works with content. Below works for me:

private void selectDir() {
    Uri d = Uri.parse("content://com.android.externalstorage.documents/document/primary:Download");
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");
    intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, d);
    startActivityForResult(intent, PICK_WDI_FILE);
}
Michael Kazarian
  • 4,376
  • 1
  • 21
  • 25