我使用导航组件 jetpak 并且在片段中需要从图库中获取图像。 我使用此代码:
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);
}
});
并在附加片段后调用 mGetContent
binding.menuFragmentCircularProfileImageview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mGetContent.launch("image/*");
// fromGallery();
}
});
到目前为止一切都很好,画廊也开得很好。 但是...
但是选择的图像没有返回。 该应用程序实际上已关闭。 我哪里做错了?或者有其他解决方案吗?
更新...
这是我的片段:
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/*");
}
});
}
}
答案 0 :(得分:0)
您可以在 onActivityResult
中使用它来获取 file
InputStream inputStream = requireActivity().contentResolver().openInputStream(uri)
String fileType = MimeTypeMap.getSingleton().getExtensionFromMimeType(requireContext().contentResolver().getType(uri))
imageFile = File.createTempFile(UUID.randomUUID().toString(), "." + fileType)
inputStream.copyStreamToFile(file)
我已经从 Kotlin 移植了这段代码,如果某些语法有误,请告诉我。
答案 1 :(得分:0)
尝试使用以下代码:
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);