0

In Android Studio:

This app must use COMTRADE files. The COMTRADE files are *.cfg *.dat and *.rio files. For example, (user.cfg, user.dat and user.rio). The main file is *.cfg file, so the user must browse by SAF to find *.cfg files, when the user select this file, the app must load other files ( *.dat and *.rio) automatically.

I can not use <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/> for android level 30

So I have to use Storage Access Framework, By below code the user browse *.cfg files and select a file like user.cfg

if(SDK_INT >= Build.VERSION_CODES.R)
{

   Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
   intent.addCategory(Intent.CATEGORY_OPENABLE);
   intent.setType("*/*");

   Act.startActivityForResult(intent, 111);

}
else
{...}

Then SAF return URI of that user.cfg file, and the app does not have any URI of other files to load.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{

   super.onActivityResult(requestCode, resultCode, data);

   if(requestCode == 111)
   {
    
      if (resultCode == Activity.RESULT_OK) 
      {
    
         if (data != null)
         {
    
            try 
            {
    
               Uri uri = null;
               uri = data.getData();
    
               InputStreamReader inputStreamReader = new InputStreamReader(getContentResolver().openInputStream(uri));
                            
               BufferedReader br = new BufferedReader(inputStreamReader);

               // I want URI of user.dat how?
                            
               InputStreamReader inputStreamReader1 = new InputStreamReader(getContentResolver().openInputStream(uri1));
                            
               BufferedReader br1 = new BufferedReader(inputStreamReader1);
    
               ....
                        
               }  
               catch (Exception e)
               {
                  e.printStackTrace();
               }

            }           

         }
    
      }

   }

}            

Please help me how can I do this? Thanks.

1 Answers1

1

Please help me how can I do this?

You cannot do this, at least by means of using ACTION_OPEN_DOCUMENT, unless you ask the user to open each of your three documents in turn. Just because the user selected a .cfg file does not mean that you have any rights to any other files, including those adjacent to it.

You could try using ACTION_OPEN_DOCUMENT_TREE and let the user choose the document tree that contains your desired files. From there, you can:

  • Use DocumentFile.fromTreeUri() to get a DocumentFile representing the tree
  • Call listFiles() on that DocumentFile to get the direct contents of that tree, in the form of a list of DocumentFile objects
  • Call getName() on each of those to get their display names, then see which names have your desired file extensions and matching base names (foo.cfg and foo.rio and foo.dat)
  • For those that you want to use, call getUri() on the DocumentFile to get a Uri to use with ContentResolver and openInputStream() to read in the content
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks a lot. So I think, First I have to list all cfg files in a folder like Downloads folder, then the user select a cfg file, and then the app find other files. Thanks for your help. – Farhad Aghajanian Jan 15 '22 at 03:46
  • Excuse me, how can I get list of files in a folder like Download folder By SAF or anything else? I do not want the user to select download folder, I want to do this first by the app. – Farhad Aghajanian Jan 15 '22 at 03:48
  • 1
    The user should select the folder if your app uses ACTION_OPEN_DOCEMENT_TREE. Since Android R/11 the Download directory can not be choosen. – blackapps Jan 15 '22 at 05:35