0

I want the user to select source folder and destination folder/destination cloud drive. So using SAF, i allow user to pick source folder and get its Uri as

content://com.android.externalstorage.documents/tree/primary%3ADownload/document/primary%3ADownload

This Uri was saved in Sharedpreference for later use. Similarly user selected Destination Folder Uri as below

content://com.android.externalstorage.documents/tree/primary%3ATest/document/primary%3ATest

This is how i saved Uri in SP

editor.putString("source", documentFile.getUri().toString()).apply();

Now i want all files will be moved from source to destination folder whenever user click on button.

   //Getting Uri saved from SP
            DocumentFile documentFile = DocumentFile.fromTreeUri(getApplicationContext(), Uri.parse(sharedPreferences.getString("source", "")));
            DocumentFile[] df = documentFile.listFiles();
            Log.i(TAG, df.length + "");

            for (DocumentFile f : df) {
                Log.i(TAG, "File path " + f.getUri().getPath());
               try {

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

Am i working in right direction as i m stuck here with do not know how to write code for copying files.

Panache
  • 1,701
  • 3
  • 19
  • 33
  • 1
    You are attempting to create a `File` object from an SAF `Uri`. That will not work. If the `"source"` preference points to a document tree, you can try to use `DocumentFile.fromTreeUri()` to create a `DocumentFile` instead of creating a `File`. However, as is being discussed [here](https://stackoverflow.com/q/59831233/115145), that may not work well without some additional code. – CommonsWare Jan 21 '20 at 12:56
  • If you want to use DocumentFile then its `documentFile.listFiles();`. When it works with DocumentFile and you wanna speed it up try using DocumenytsContract. – blackapps Jan 21 '20 at 12:58
  • `This is how i saved Uri in SP editor.putString("source", documentFile.getUri().toString()).apply();` That is strange! As when you let the user pic a directory with saf wou will get an uri (which you can save) in onActivityResult(). And no documentfile. – blackapps Jan 21 '20 at 13:02
  • ` destination folder/destination cloud drive. ` There is nothing 'cloudy' on the destination uri you posted. – blackapps Jan 21 '20 at 13:05
  • Can you guide a best practice to achieve what i m trying to. – Panache Jan 21 '20 at 14:28
  • Finally i achieved i wanted using https://stackoverflow.com/questions/36023334/android-how-to-use-new-storage-access-framework-to-copy-files-to-external-sd-c/43051555 – Panache Jan 21 '20 at 16:32

0 Answers0