首先生成一个URI:
private Uri getUri(String dir, String suffix) {
File root = new File(requireActivity().getCacheDir(), dir);
if (!root.exists()) {
root.mkdirs();
}
File file = new File(root, System.currentTimeMillis() + suffix);
Uri uri = FileProvider.getUriForFile(requireActivity(), requireActivity().getPackageName() + ".fileprovider2", file);
return uri;
}
然后直接使用:
@Override
protected void initView() {
binding.test6FragmentBtn.setText("拍照");
binding.test6FragmentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = getUri("test_Picture", ".png");
requireActivity().getActivityResultRegistry().register("takePicture", new ActivityResultContracts.TakePicture(), new ActivityResultCallback<Boolean>() {
@Override
public void onActivityResult(Boolean result) {
One_Fragment_02 one_fragment_02 = new One_Fragment_02();
one_fragment_02.show(getChildFragmentManager(), One_Fragment_02.class.getSimpleName());
List<Uri> uriList = new ArrayList<>();
if (result) {
uriList.add(uri);
}
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(One_Fragment_02.BUNDLE_KEY, (ArrayList<? extends Parcelable>) uriList);
getChildFragmentManager().setFragmentResult(One_Fragment_02.REQUEST_KEY, bundle);
}
}).launch(uri);
}
});
binding.test6FragmentBtn2.setText("录制视频");
binding.test6FragmentBtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = getUri("test_Video", ".mp4");
requireActivity().getActivityResultRegistry().register("takeVideo", new ActivityResultContracts.TakeVideo(), new ActivityResultCallback<Bitmap>() {
@Override
public void onActivityResult(Bitmap result) {
Eight_Fragment_02 eight_fragment_02 = new Eight_Fragment_02();
eight_fragment_02.show(getChildFragmentManager(), Eight_Fragment_02.class.getSimpleName());
Bundle bundle = new Bundle();
bundle.putString(Eight_Fragment_02.BUNDLE_KEY, uri.toString());
getChildFragmentManager().setFragmentResult(Eight_Fragment_02.REQUEST_KEY, bundle);
}
}).launch(uri);
}
});
binding.test6FragmentBtn3.setText("申请单个权限");
binding.test6FragmentBtn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requireActivity().getActivityResultRegistry().register("permission", new ActivityResultContracts.RequestPermission(), new ActivityResultCallback<Boolean>() {
@Override
public void onActivityResult(Boolean result) {
if (result) {
Loge.e("获取到权限");
} else if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
Loge.e("展示给用户,申请这个权限的必要性,只有一次展示机会");
} else {
Loge.e("用户没有给权限,拜拜");
// requireActivity().finish();
}
}
}).launch(Manifest.permission.CAMERA);
}
});
binding.test6FragmentBtn4.setText("申请多个权限");
binding.test6FragmentBtn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requireActivity().getActivityResultRegistry().register("permissions", new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {
@Override
public void onActivityResult(Map<String, Boolean> result) {
for (Map.Entry<String, Boolean> entry : result.entrySet()) {
if (entry.getValue()) {
Loge.e(entry.getKey() + ":" + entry.getValue());
} else {
Loge.e("用户没有同意这个权限:" + entry.getKey());
}
}
}
}).launch(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA, Manifest.permission.READ_CONTACTS});
}
});
binding.test6FragmentBtn5.setText("文件选择器");
binding.test6FragmentBtn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requireActivity().getActivityResultRegistry().register("GetContent", new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
@Override
public void onActivityResult(Uri result) {
Loge.e(result.toString());
}
}).launch("image/*");
}
});
binding.test6FragmentBtn6.setText("一次选多个文件");
binding.test6FragmentBtn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requireActivity().getActivityResultRegistry().register("", new ActivityResultContracts.GetMultipleContents(), new ActivityResultCallback<List<Uri>>() {
@Override
public void onActivityResult(List<Uri> result) {
Loge.e(result.toString());
}
}).launch("image/*");
}
});
binding.test6FragmentBtn7.setText("跳转下一个activity");
binding.test6FragmentBtn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requireActivity().getActivityResultRegistry().register("StartActivityForResult", new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
}
}).launch(new Intent(getActivity(), Test2_Fragment_Activity.class));
}
});
}
效果图如下:
附上2个展示 Video和 Picture 的fragment代码:
One_Fragment_02这个是展示图片的:
public class One_Fragment_02 extends Util_BaseFullBottomSheetFragment<ItemLayouBinding> {
public static final String REQUEST_KEY = "uri";
public static final String BUNDLE_KEY = "list_uri";
@Override
protected int getLayoutId() {
return R.layout.item_layou;
}
@Override
protected void initView() {
getParentFragmentManager().setFragmentResultListener(REQUEST_KEY, this, new FragmentResultListener() {
@Override
public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) {
LinearLayout linearLayout = (LinearLayout) binding.itemTv.getParent();
List<Uri> uriList = result.getParcelableArrayList(BUNDLE_KEY);
for (Uri uri : uriList) {
ImageView imageView = new ImageView(getContext());
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
linearLayout.addView(imageView, layoutParams);
imageView.setImageURI(uri);
}
ScrollView scrollView = (ScrollView) linearLayout.getParent();
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (scrollView.getScrollY() > 0)
scrollView.requestDisallowInterceptTouchEvent(true);
return false;
}
});
}
});
}
}
Eight_Fragment_02这个是展示video视频的:
public class Eight_Fragment_02 extends Util_BaseFullBottomSheetFragment<VideoViewBinding> {
public static final String REQUEST_KEY="video";
public static final String BUNDLE_KEY="video_key";
@Override
protected int getLayoutId() {
return R.layout.video_view;
}
@Override
protected void initData() {
getParentFragmentManager().setFragmentResultListener(REQUEST_KEY, this, new FragmentResultListener() {
@Override
public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) {
Loge.e(requestKey);
Loge.e(result.getString(BUNDLE_KEY, "null"));
Uri uri = Uri.parse(result.getString(BUNDLE_KEY));
binding.test6FragmentVideoview.setVideoURI(uri);
binding.test6FragmentVideoview.requestFocus();
binding.test6FragmentVideoview.start();
}
});
binding.test6FragmentVideoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Loge.e("播放完成");
}
});
}
}
getActivityResultRegistry非常简单的代码就能实现了 拍照,录制视频,申请权限,操作文档等这些。无需提前在onCreate注册 registerForActivityResult