Skip to content

Java Multiple Uri

Murillo Comino edited this page Jul 30, 2020 · 2 revisions

Multiple Uri

💫 Initialization

The implementation of the Listener you can implement it within the scope of the class, as shown below:

     public class MainActivity extends AppCompatActivity implements HandlePathOzListener.MultipleUri {
      //
      }

Alt+Enter to implement the methods, we will discuss the methods later in the topic Controller. Implement handlePathOz in your onCreate() method, as shown below:

    private HandlePathOz handlePathOz;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Initialize HandlePathOz
        //context, listener
        handlePathOz = HandlePathOz(this, this)
    }

After selecting the desired files (The sample application has the entire step) in onActivityResult leave as follows:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_OPEN_GALLERY && resultCode == RESULT_OK) {
            //This extension retrieves the path of all selected files without treatment.
            listUri = getListUri(data);

            //with the list you can update some recyclerview and switch to the method that handles Uri's.


            //set list of the Uri to handle
            //in concurrency use:
            // 1                -> for tasks sequentially
            //greater than 1    -> for the number of tasks you want to perform in parallel.
            //Nothing           -> for parallel tasks - by default the value is 10
            handlePathOz.getListRealPath(listUri);
            // handlePathOz.getListRealPath(listUri, 1)

            //show Progress Loading
        }
    }

🎮 Controller

We have two methods in the listeners, one of which is optional:

        //On Completion (Sucess or Error)
        //If there is a cancellation or error, the entire task that was handled will be returned in the list.
       @Override
       public void onRequestHandlePathOz(@NonNull List<PathOz> listPathOz, Throwable tr) {
            //Hide Progress
            //Update the recyclerview with the list
            //Update the adapter
            List<Uri> listUri = new ArrayList<>();
            for (int i = 0; i < listPathOz.size(); i++) {
                Uri uri = Uri.parse(listPathOz.get(i).getPath());
                listUri.add(uri);
            }
            yourAdapter.updateListChanged(listUri);
    
            //Handle Exception (Optional)
            if (throwable != null) {
                Toast.makeText(this, throwable.getMessage(), Toast.LENGTH_SHORT).show();
            }
       }

       //This method is Optional
       @Override
       public void onLoading(int currentUri) {
           //Update UI with the current Uri
           //progressLoading.setText(currentUri + "/" + listUri.size());
       }

☁️ Cloud files and Unknown Providers

If the selected file was from Dropbox,Google Drive, OneDrive or an unknown file provider, it will then be copied/created in InternalStorage/Android/data/your.package.name/files/Temp/sameFileNameAndExtension When you want to delete the generated files call:

   handlePathOz.deleteTemporaryFiles()

💣 Cancel the tasks

There are two methods for canceling tasks, cancelTask() and onDestroy().

handlePathOz.cancelTask() -> Can be called as a button action for canceling or by progressBar (As shown in the demo application). In the cancellation of the task by this method, all Uri that was treated will be passed in the onRequestHandlePathOz() method.

handlePathOz.onDestroy() -> It can be called with the Activity or fragment's onDestroy() method. This method destroys the task and its cancellation does not update anything and cannot be restarted. Example of use:

    @Override
    public void onDestroy() {
        handlePathOz.onDestroy();
        //You can delete the temporary files here as well.
        super.onDestroy();
    }