gpt4 book ai didi

java - 如何将图像从一个 fragment 发送到另一个 fragment ?

转载 作者:行者123 更新时间:2023-12-04 07:39:42 24 4
gpt4 key购买 nike

我阅读了这些,但错误仍然存​​在:
How to pass selected image from one fragment to another fragment
Passing image from one fragment to a another fragment and display image in that fragment
我创建了一个 Activity ,它将数据和图像从一个 fragment 传输到另一个 fragment ,数据解析成功,但是当我发送图像时,它说

2021-05-16 04:08:52.127 26026-26026/com.example.mcqapp E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /document/image:11600: open failed: ENOENT (No such file or directory)


这是我的 fragment 代码
if (imageView.getVisibility() == View.VISIBLE) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK && data != null) {

filePath = data.getData();
path_New = filePath.getPath();
try {
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(),filePath);
imageView.setImageBitmap(bitmap);
}catch (Exception e){
e.printStackTrace();
}
}

bundle.putString("image",path_New);
second mfragment=new second();
mfragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment1, mfragment).commit();
接收图像的第二个 fragment 中的代码:
Bundle bundle = getArguments();
String imagePath = bundle.getString("image");


Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
imageView.setImageBitmap(bitmap);

最佳答案

好的,我解决了,我将 uri 转换为字符串,然后在第二个 fragment 中将 String 转换为

urifilePath = data.getData();
String path = filePath.toString();
bundle.putString("image",path);
第二个 fragment
String imagePath = bundle.getString("image");
Uri myUri = Uri.parse(imagePath);
imageView.setImageURI(myUri);

关于java - 如何将图像从一个 fragment 发送到另一个 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67551860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com