如何将图像从一个片段发送到另一个片段?
How to send image from one fragment to another?
我阅读了这些内容,但仍然存在错误:
How to pass selected image from one fragment to another fragment
我创建了一个 activity 将数据和图像从一个片段传输到另一个片段,数据解析成功但是当我发送图像时它说
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)
这是我的片段代码
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();
接收图像的第二个片段中的代码:
Bundle bundle = getArguments();
String imagePath = bundle.getString("image");
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
imageView.setImageBitmap(bitmap);
好的,我解决了,我将 uri 转换为字符串,然后在第二个片段中我将字符串转换为
urifilePath = data.getData();
String path = filePath.toString();
bundle.putString("image",path);
第二个片段
String imagePath = bundle.getString("image");
Uri myUri = Uri.parse(imagePath);
imageView.setImageURI(myUri);
我阅读了这些内容,但仍然存在错误:
How to pass selected image from one fragment to another fragment
我创建了一个 activity 将数据和图像从一个片段传输到另一个片段,数据解析成功但是当我发送图像时它说
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)
这是我的片段代码
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();
接收图像的第二个片段中的代码:
Bundle bundle = getArguments();
String imagePath = bundle.getString("image");
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
imageView.setImageBitmap(bitmap);
好的,我解决了,我将 uri 转换为字符串,然后在第二个片段中我将字符串转换为
urifilePath = data.getData();
String path = filePath.toString();
bundle.putString("image",path);
第二个片段
String imagePath = bundle.getString("image");
Uri myUri = Uri.parse(imagePath);
imageView.setImageURI(myUri);