我使用导航组件jetpak,并且在 fragment 中需要从图库获取图像。 我使用这段代码:
ActivityResultLauncher<String> mGetContent = registerForActivityResult(new ActivityResultContracts.GetContent(),
new ActivityResultCallback<Uri>() {
@Override
public void onActivityResult(Uri uri) {
Constants.toast("return!");
imageFile = new File(getRealPathFromURI(uri));
binding.menuFragmentCircularProfileImageview.setImageURI(uri);
}
});
并在附加 fragment 后调用 mGetContent
binding.menuFragmentCircularProfileImageview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mGetContent.launch("image/*");
// fromGallery();
}
});
到目前为止一切都很好,画廊开放顺利。 但是...
但是所选图像没有返回。 该应用程序实际上已关闭。 我哪里做错了?或者还有其他解决方案吗?
已更新...
这是我的 fragment :
public class MenuFragment extends Fragment implements LogoutDialog.Listener {
private FragmentMenuBinding binding;
private NavController navController = null;
private UserData userData;
private File imageFile;
private final androidx.activity.result.ActivityResultLauncher<String> getContent = registerForActivityResult(new ActivityResultContracts.GetContent(),
new ActivityResultCallback<Uri>() {
@Override
public void onActivityResult(Uri uri) {
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(requireActivity().getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
binding.menuFragmentCircularProfileImageview.setImageBitmap(bitmap);
}
});
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
navController = Navigation.findNavController(requireActivity(), R.id.main_activity_nav_host_fragment);
binding = DataBindingUtil.inflate(inflater , R.layout.fragment_menu, container, false);
return binding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
assert getArguments() != null;
userData=MenuFragmentArgs.fromBundle(getArguments()).getUserdata();
operation();
}
private void operation(){
binding.menuFragmentCircularProfileImageview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getContent.launch("image/*");
}
});
}
}
最佳答案
尝试使用以下代码:
private ActivityResultLauncher startForResultFromGallery = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK){
try {
if (result.getData() != null){
Uri selectedImageUri = result.getData().getData();
Bitmap bitmap = BitmapFactory.decodeStream(getBaseContext().getContentResolver().openInputStream(selectedImageUri));
// set bitmap to image view here........
binding.menuFragmentCircularProfileImageview.setImageBitmap(bitmap)
}
}catch (Exception exception){
Log.d("TAG",""+exception.getLocalizedMessage());
}
}
}
});
并在按钮内调用上面的按钮,如下所示:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startForResultFromGallery.launch(intent);
关于android - 如何从 fragment 中的图库获取图像(jetpack导航组件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67156608/