Skip to content

Kotlin Multiple Uri

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

Multiple Uri

💫 Initialization

1.1 - In Kotlin for the implementation of the Listener you can implement it within the scope of the class, as shown below, or also as shown in item 1.2:

  class MainActivity : AppCompatActivity(), 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 lateinit var handlePathOz: HandlePathOz

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //Initialize HandlePathOz
        //context, listener
        handlePathOz = HandlePathOz(this, this)
    }

1.2 - You can also implement the Listener when initializing the class, without having to implement it within the scope of the class:

      private lateinit var handlePathOz: HandlePathOz
      private val listener = object: HandlePathOzListener.MultipleUri{
      //implement methods
      }
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          //Initialize HandlePathOz
          //context, listener
          handlePathOz = HandlePathOz(this, listener)
      }

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

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if ((requestCode == REQUEST_OPEN_GALLERY) and (resultCode == Activity.RESULT_OK)) {
            //This extension retrieves the path of all selected files without treatment.
            val listUri = data.getListUri()

            //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 fun onRequestHandlePathOz(listPathOz: List<PathOz>, tr: Throwable?) {
          //Hide Progress
          //Update the recyclerview with the list
          yourAdapter.updateListChanged(listPathOz.map { uri -> Uri.parse(uri.path) })
  
          //Handle any Exception (Optional)
          tr?.let {
              Toast.makeText(this, "${it.message}", Toast.LENGTH_SHORT).show()
          }
      }
  
      //This method is Optional
      override fun onLoading(currentUri: Int) {
          //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 fun onDestroy() {
        handlePathOz.onDestroy()
        //You can delete the temporary files here as well.
        super.onDestroy()
    }