Skip to content

Java Single Uri

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

Single 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.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 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) {
            Uri uri = data.getData();
            if (uri != null) {
                //set Uri to handle
                handlePathOz.getRealPath(uri);
                //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.
       @Override
       public void onRequestHandlePathOz(@NonNull PathOz pathOz, Throwable tr) {
            //Hide Progress
            
            //Now you can work with real path:
            Toast.makeText(this, "The real path is: " + pathOz.getPath + "\n The type is: " + path.getType, Toast.LENGTH_SHORT).show();
    
            //Handle Exception (Optional)
            if (throwable != null) {
                Toast.makeText(this, throwable.getMessage(), 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
    public void onDestroy() {
        handlePathOz.onDestroy();
        //You can delete the temporary files here as well.
        super.onDestroy();
    }