简体   繁体   中英

How to send image from one fragment to another?

I read these but an error persists:

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

I have created an activity which is transferring data and image from one fragment to another, data is parsing successfully but when I am sending image it says

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)

This is my code for fragment first

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();

Code in second fragment to receive image:

Bundle bundle = getArguments();
String imagePath = bundle.getString("image");


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

Alright,I solved it, i converted uri to string and then in second fragment i converted String to

urifilePath = data.getData();
String path = filePath.toString();
bundle.putString("image",path);

Second Fragment

String imagePath = bundle.getString("image");
Uri myUri = Uri.parse(imagePath);
imageView.setImageURI(myUri);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM