Skip to content

Kotlin Single Uri

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

Single 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.SingleUri {
   //...
   }

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.SingleUri{
      //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)) {
            data?.data?.also { it ->
                //set uri to handle
                handlePathOz.getRealPath(it)
                //show Progress Loading
            } 
        }
    }

🎮 Controller

Override Listeners

      //On Completion (Sucess or Error)
      //If there is a cancellation or error.
      override fun onRequestHandlePathOz(pathOz: PathOz, tr: Throwable?) {
          //Hide Progress

          //Now you can work with real path:
          Toast.makeText(this, "The real path is: ${pathOz.path} \n The type is: ${path.type}", Toast.LENGTH_SHORT).show()
  
          //Handle any Exception (Optional)
          tr?.let {
              Toast.makeText(this, "${it.message}", Toast.LENGTH_SHORT).show()
          }
      }

☁️ 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).

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()
    }