Android Android development: use ActivityResultLauncher to jump pages, pass parameters, take pictures or select files, and call system applications to open various types of specified files

ActivityResultLauncher is a new way officially recommended by Android to replace startActivityForResult. Through it, it is very convenient to call the system Intent to take pictures, or select local files.

This article is divided into 5 chapters:

1. Define ActivityResultLauncher

2. Register Launcher

3. Call the system Intent

Fourth, use FileProvider to copy a file

5. Use system applications to open various types of specified files

Feel free to leave me a message, or write me an email:

[email protected]

[email protected]

1. Define ActivityResultLauncher

Before we need to call the system Intent, we need to define the required Launcher:

protected ActivityResultLauncher activityResultLauncher;
protected ActivityResultLauncher takePhotoLauncher;
protected ActivityResultLauncher selectImageLauncher;
protected ActivityResultLauncher selectFileLauncher;

Here I define 4 different Launchers, which are used to handle ordinary Intent jumps, take pictures, select existing pictures in the album, and select files in the phone.

activityResultLauncher: handle common Intent jumps, and carry parameters back and forth;

takePhotoLauncher: open the camera to take pictures and get the pictures;

selectImageLauncher: open the system Intent to select the picture on the phone;

selectFileLauncher: Open the system Intent to select files in the phone.

2. Register Launcher

Before official use, we need to register different Launchers for different needs.

2.1 Register common Intent jump and callback

this.activityResultLauncher = this.registerForActivityResult(
    new ActivityResultContracts.StartActivityForResult(),
    new ActivityResultCallback<ActivityResult>(){

        @Override
        public void onActivityResult(ActivityResult result) {
            //使用result.getResultCode()来获取返回的result code数据
            //使用result.getData.getStringExtra()或其它方法,可以获取返回参数
        }
    }
);

Note the following in the above code snippet:

ActivityResultContracts.StartActivityForResult()

The corresponding Callback method is:

ActivityResultCallback<ActivityResult>(){

   @Override
   public void onActivityResult(ActivityResult result) {
      //Use result.getResultCode() to get the returned result code data
      //Use result.getData.getStringExtra() or other methods to get return parameters
   }
}

2.2 Register camera Intent and callback

this.takePhotoLauncher = this.registerForActivityResult(
    new ActivityResultContracts.TakePicture(), 
    new ActivityResultCallback<Boolean>() {
        @Override
        public void onActivityResult(Boolean result) {
            if(result){
                //result 是布尔值,为true时表示拍照成功
            }
        }
    }
);

Note the following in the above code snippet:

ActivityResultContracts.TakePicture()

The corresponding Callback method is:

ActivityResultCallback<Boolean>() {
    @Override
    public void o

Guess you like

Origin blog.csdn.net/freezingxu/article/details/124953918