0

My app has to save a new excel file in a user-selected directory.

The user select the directory with OpenDocumentTree picker directory picker. This intent returns an Uri.

I'm not able to transform Uri in path for the write function. How can I do? Thanks a lot.

final ActivityResultLauncher<Uri> mDirRequest = registerForActivityResult(
        new ActivityResultContracts.OpenDocumentTree(),
        new ActivityResultCallback<Uri>() {
            @Override
            public void onActivityResult(Uri result) {

                path = somethingIdontknow;

                try {
                    File file = new File(path, "goofy.xlsx");
                    FileOutputStream out = new FileOutputStream(file);
                    workbook.write(out);
                    out.close();

                }
                catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

......

mDirRequest.launch(Uri.parse(DocumentsContract.EXTRA_INITIAL_URI));

I tried to implement a lot of suggestions from stackoverflow, but I've not resolved the problem.

linuxp
  • 13
  • 3

2 Answers2

1

You will not transform an uri in a path to begin with.

You can create a DocumentFile instance for the obtained uri.

After that use DocumentFile:createFile() to get an uri for the created file for which you open an InputStream and write to it.

blackapps
  • 8,011
  • 2
  • 11
  • 25
0

you can use the ContentResolver and the openFileDescriptor method to get a ParcelFileDescriptor object.

Here is an example how it can looks like:

final ActivityResultLauncher<Uri> mDirRequest = registerForActivityResult(
    new ActivityResultContracts.OpenDocumentTree(),
    new ActivityResultCallback<Uri>() {
        @Override
        public void onActivityResult(Uri result) {

            Uri fileUri = Uri.withAppendedPath(result, "pippo.xlsx");

            ContentResolver resolver = getContentResolver();
            try {
                OutputStream outputStream = resolver.openOutputStream(fileUri);
                workbook.write(outputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
  • Sorry. You select a directory and try to write to it. That will not go as you cannot open an outputstream for a directory. – blackapps Jan 02 '23 at 12:26
  • yes you are right i didnt tested the code. i edited the code now and chenge it – Muhamad Kheder Jan 02 '23 at 12:45
  • Thank you for your suggest, but I got: E/AndroidRuntime: FATAL EXCEPTION: main Process: it.linuxp.psswordlist, PID: 16956 java.lang.IllegalArgumentException: Invalid URI: content://com.android.externalstorage.documents/tree/primary%3ATryagain/goofy.xlsx Where is the error? – linuxp Jan 02 '23 at 14:39
  • are you sure that you have the permission to read the file. and the file does exists? if yes then you should be sure to use the getContentResolver method of your Activity or Fragment – Muhamad Kheder Jan 02 '23 at 15:14
  • The file doesn't exist. It's a new file. I'm using OpenDocumentTree to select the directory where I'm going to save a new file. Sorry, maybe my first question is ambigouos... – linuxp Jan 02 '23 at 15:33
  • `. i edited the code now and chenge it` It is still nonsense, impossible not working code. – blackapps Jan 02 '23 at 15:47
  • Sorry, I also tried manually creating the file (in /Downolad/Puppi/goofy.xlsx) before the launch of my app. I set those permissions: Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE. The result is still: ava.lang.IllegalArgumentException: Invalid URI: content://com.android.externalstorage.documents/tree/primary%3ADownload%2FPuppi/goofy.xlsx – linuxp Jan 02 '23 at 16:19