1

I need to open a specific folder in Android 10, so that only the files inside that folder can be seen in my app.

I have tried this:

            try {
                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                File f = new File(getExternalFilesDir(null) + "/MyFolder");
                Uri uri = FileProvider.getUriForFile(getApplicationContext(),getApplicationContext().getPackageName()+".provider",f);
                Toast.makeText(MainActivity.this,f.getPath(),Toast.LENGTH_LONG).show();
                intent.setDataAndType(uri,"text/plain");
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                startActivity(intent);
            }
            catch (Exception e) {
                Log.d("Exception",e.toString());
                Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
            }

But, it opens the entire tree.

What I actually want is to open that folder. The files in the folder should be openable by other apps. Any help will be appreciated.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Samudra Ganguly
  • 637
  • 4
  • 24
  • You are not alone today: https://stackoverflow.com/questions/65001666/cant-control-the-initial-directory-where-the-saf-ui-should-start – blackapps Nov 25 '20 at 10:12
  • `The files in the folder should be openable by other apps.` ?? What do you mean by that? – blackapps Nov 25 '20 at 10:12
  • `so that only the files inside that folder can be seen in my app.` Not possible. The user can always browse to all other locations. – blackapps Nov 25 '20 at 10:13
  • No .... I meant to say that my app should be able to open only those text files that are inside MyFolder. – Samudra Ganguly Nov 25 '20 at 11:08

1 Answers1

0

I need to open a specific folder in Android 10, so that only the files inside that folder can be seen in my app

Sorry, but that is not an option.

The closest thing to what you want is EXTRA_INITIAL_URI. However:

  • It takes a document or tree Uri that you obtained previously from the Storage Access Framework, not a FileProvider Uri; and

  • It does not prevent users from navigating elsewhere — it just controls the starting point

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • As far as I know, it is allowed in API level 26 and above. What about API 25 and below? – Samudra Ganguly Nov 27 '20 at 05:20
  • @SamudraGanguly: Sorry, but I do not know what "it" is in "it is allowed". – CommonsWare Nov 27 '20 at 11:46
  • "it" means specifying starting location. – Samudra Ganguly Nov 30 '20 at 06:32
  • and can you kindly provide some more help regarding how exactly I can achieve it? – Samudra Ganguly Nov 30 '20 at 09:44
  • @SamudraGanguly: "As far as I know, it is allowed in API level 26 and above" -- only in the form of `EXTRA_INITIAL_URI`, as I mentioned in my answer. – CommonsWare Nov 30 '20 at 12:16
  • Can you kindly elaborate a bit how to use the appropriate uri? – Samudra Ganguly Nov 30 '20 at 12:40
  • @SamudraGanguly: Well, in the beginning, you do not have one. So you ask the user to pick something without `EXTRA_INITIAL_URI`. When you get the `Uri` of the user's selection, in addition to whatever else you needed to do with it, save a copy of it (e.g., in `SharedPreferences`). Later, when you need to ask the user to pick a different location, you can include that previous `Uri` in `EXTRA_INITIAL_URI`. – CommonsWare Nov 30 '20 at 12:49
  • To be honest, I am not so familiar with Uri as I am a beginner. I want to make a text editor app (something like notepad). The app will save the texts as .txt files (which I have successfully implemented). Now, I want an **OPEN** button that will open the folder inside which my files are stored. Now, I will definitely want my app to open the folder in which I have saved the files. But I am unable to do it. – Samudra Ganguly Nov 30 '20 at 13:16
  • @SamudraGanguly: "Now, I will definitely want my app to open the folder in which I have saved the files" -- if you are looking for system UI to do that, it would only be possible if you asked the user for that folder previously using `ACTION_OPEN_DOCUMENT_TREE`. – CommonsWare Nov 30 '20 at 13:22