1

i want to select image in one fragment and display it in another fragment.
how can i do this.
my code is this but not working

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            startActivityForResult(gallery, 100);
        }
    });


@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    Uri imageUri;
    if (resultCode == RESULT_OK && requestCode == 100){
        imageUri = data.getData();
        file1path = imageUri.getPath();
        TVfrontimg.setText(file1path);
    }
}

and i send this using bundle

Bundle bundle = new Bundle();
bundle.putString("image1", file1path);

registerPartThreeFragment.setArguments(bundle);
transaction.replace(R.id.FLcontainer, registerPartThreeFragment);
transaction.addToBackStack(null);
transaction.commit();

in another fragment

String imgPath = getArguments().getString("image1");
Bitmap bitmap = BitmapFactory.decodeFile(String.valueOf(new File(imgPath)));
imageview.setImageBitmap(bitmap);

but its not working.please help.


its giving me this error

2020-05-30 00:16:20.808 8995-8995/com.example.deliveryapp E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /raw/storage/emulated/0/DCIM/Camera/IMG_20200319_173033.jpg (No such file or directory)

mimoh
  • 11
  • 5

3 Answers3

0

Please use getDataString() instead of getData()

if (resultCode == RESULT_OK && requestCode == 100){
        Uri imageUri = Uri.parse(data.getDataString());
        //imageUri = data.getData();
        file1path = imageUri.getPath();
        TVfrontimg.setText(file1path);
}

And while setting it to the ImageView use ContentResolver and input stream like so:

ContentResolver cR = getApplication().getContentResolver();
        try {
            InputStream ip = cR.openInputStream(imageUri);
            Bitmap bmp = BitmapFactory.decodeStream(ip);
            imageView.setImageBitmap(bmp)
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

You have to do this because you are getting a contentUri which is different from a file Uri.

Hope this helps.

hushed_voice
  • 3,161
  • 3
  • 34
  • 66
0
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);

This code you are want showing for image

You should look find real path

Look at answers : Get filepath and filename of selected gallery image in Android

android get real path by Uri.getPath()

0

In your first fragment

if (resultCode == RESULT_OK && requestCode == 100){
               val bundle = Bundle()
               bundle.putString("Image",your_image_path)
            findNavController().navigate(R.id.your_destination_fragment, bundle)

}

in second fragment

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    arguments?.let {
             image= it.getString("Image")
             bitmp= MediaStore.Images.Media.getBitmap(
                    requireActivity().contentResolver,
                     Uri.parse(image)

}

and set your bitmap in imageview

imgBitmap.setImageBitmap(bmp)

Yash Shah
  • 52
  • 4