androidurisaf

I Use Storage Access Framework to browse a file link f.cfg, when you select the file, the app need to load another file like f.dat automatically, how?


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.


Solution

  • 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: