0

I am working on a project which allow user to select excel file from any folder, issue is faced when I am trying to access path by following code.

My Intent

   int currentVer = android.os.Build.VERSION.SDK_INT;
                    if( currentVer == 29 ) {
                        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                        intent.addCategory(Intent.CATEGORY_OPENABLE);
                        intent.setType("*/*");
                        startActivityForResult(intent, 1);
                    }
               else if ( currentVer == 30 ) {
                        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                        intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
                                | Intent.FLAG_GRANT_READ_URI_PERMISSION | 
                                  Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

                        startActivityForResult(intent, 1);
                    }

My activity result code

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


        try {
        super.onActivityResult(requestCode, resultCode, data);


        if ( data != null) {
            if (resultCode == Activity.RESULT_OK) {
                Uri uri = data.getData();
                int takeFlags = data.getFlags();
                takeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
                        Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    this.getContentResolver().takePersistableUriPermission(uri, takeFlags);

                }
                String Name = file_name(uri);
                String src = uri.getPath();
                File file = new File( src );


                if(ExcelUtils.checkIfExcelFile(Name))
                {

                    excelUtils.readExcel(file,FirstRowHeader); //Read Excel file content
                }
                 
            }
       }


    } catch (Exception ex) {
        
    }}

If I use "/sdcard/Download/Demodown.xls" this static path file is accessible, but by following line it errored out

      File file = new File( src ); 

Error => java.io.FileNotFoundException: content:/com.android.providers.downloads.documents/document/17

MKhan
  • 1
  • 2
  • "when I am trying to access path" -- a `Uri` is not a file. Use `ContentResolver` and `DocumentFile` to work with the `Uri` that you get. – CommonsWare Oct 24 '21 at 23:43
  • As my code needs a File as my input working, is it possible to get a File type by ContentResolver or DocumentFile. – MKhan Oct 25 '21 at 10:48
  • You would need to copy the bytes of the content to some file that you control. Or, switch to some other library that can work with `InputStream`. – CommonsWare Oct 25 '21 at 10:50
  • @CommonsWare Thanks a lot. One more thing can you please guide in allowing access to user for selection of excel file from phone and SDcard storage in Android 11. – MKhan Oct 25 '21 at 18:23
  • I do not know what you mean by that, sorry. `ACTION_OPEN_DOCUMENT` would seem to cover your scenario. – CommonsWare Oct 25 '21 at 18:35
  • @CommonsWare As I have searched that 'ACTION_OPEN_DOCUMENT' is depreciated in Android 11 (https://developer.android.com/training/data-storage/shared/documents-files). But by using 'ACTION_OPEN_DOCUMENT_TREE' I am unable to get excel file from Document folder of external or SDcard storage. – MKhan Oct 25 '21 at 18:42
  • "As I have searched that 'ACTION_OPEN_DOCUMENT' is depreciated in Android 11" -- no, it is not. The word "deprecated" does not appear on that page, nor is it marked as deprecated in [its JavaDocs entry](https://developer.android.com/reference/android/content/Intent#ACTION_OPEN_DOCUMENT). The documentation that you linked to [specifically recommends using `ACTION_OPEN_DOCUMENT`](https://developer.android.com/training/data-storage/shared/documents-files#open-file). – CommonsWare Oct 25 '21 at 19:47
  • @CommonsWare ok got it, Thanks for answering these questions. – MKhan Oct 25 '21 at 20:03

0 Answers0