import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
public class Get_Video {
public interface ActionListener{
public void get_video_uri(Uri video_uri);
public void cancel_select();
}
public ActionListener actionListener=null;
private ActivityResultLauncher<Intent> get_Launcher;
public Get_Video(AppCompatActivity activity){
get_Launcher = activity.registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
Uri selectedVideoUri = data.getData();
if(actionListener!=null)
actionListener.get_video_uri(selectedVideoUri);
}else{
if(actionListener!=null)
actionListener.cancel_select();
}
}
});
}
public void select_video(){
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
get_Launcher.launch(intent);
}
}
使用方法:
videoView=findViewById(R.id.video_view);
get_video=new Get_Video(this);
get_video.actionListener=new Get_Video.ActionListener() {
@Override
public void get_video_uri(Uri video_uri) {
videoView.setVideoURI(video_uri);
videoView.start();
}
@Override
public void cancel_select() {
}
};
get_video.select_video();
沒有留言:
張貼留言