registerForActivityResult抛出异常处理

register while current state is RESUMED. LifecycleOwners must call register before they are STARTED

意思就是registerForActivityResult必须在生命周期STARTED之前调用

解决:将registerForActivityResult的创建移动到onCreate()中去

private ActivityResultLauncher<Intent> intentActivityResultLauncher;

在onCreate()写:

intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),result->{
            //此处是跳转的result回调方法
            Log.d(TAG,result.getData().getStringExtra("dat"));
            if (result.getData() != null && result.getResultCode() == Activity.RESULT_OK) {

            }
        });

需要跳转页面的时候:

 private void createVideo() {
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        //使用0,录制1分钟大概内存是几兆
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        //当在这里设置时长之后,录制到达时间,系统会自动保存视频,停止录制
        intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 61);
        // 限制大小 限制视频的大小,这里是100兆。当大小到达的时候,系统会自动停止录制
        intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 1024 * 1024 * 100);
        //在这里有录制完成之后的操作,系统会默认把视频放到照片的文件夹中
        //startActivityForResult(intent, 11);
        intentActivityResultLauncher.launch(intent);
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容