Pass an image from one fragment to another

0

I have an application that in a fragment takes pictures with the camera or the load from the gallery, until there all right, it reaches the camera and also lets me select the photo from the gallery. The problem is that I want to show next the photo taken / image selected in another fragment with a couple of buttons In summary, I would like to pass the photo as a parameter to another fragment, I read that it can be with bundle , but it is very confusing for me to do it and where.

I leave the code that calls the camera, the gallery, and where I keep the image

private void navigateToCamera() {
    Intent nativeCameraIntent = new Intent().setClass(getActivity(), NativeCameraActivity.class);
    // Start the Intent
    startActivityForResult(nativeCameraIntent, RESULT_LOAD_PHOTO);
}

public void loadImageFromGallery() {
    // Create intent to Open Image applications like Gallery, Google Photos
    Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // Start the Intent
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG);

}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // When an Image is picked
        if (requestCode == RESULT_LOAD_IMG && resultCode == getActivity().RESULT_OK && null != data) {
            // Get the Image from data
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            // Get the cursor
            Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imgDecodableString = cursor.getString(columnIndex);
            cursor.close();

            ImageView imgView = (ImageView) getView().findViewById(R.id.faceImg);
            // Set the Image in ImageView after decoding the String
           imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));

        } else if (requestCode == RESULT_LOAD_PHOTO && resultCode == getActivity().RESULT_OK && null != data) {
            ImageView imgView = (ImageView) getView().findViewById(R.id.faceImg);
            // Set the Image in ImageView after decoding the String
            imgView.setImageBitmap(BitmapFactory.decodeFile(data.getStringExtra("imageUri")));
        } else {
            Toast.makeText(getContext(), R.string.no_photo_selected, Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(getContext(), "Something went wrong", Toast.LENGTH_LONG).show();
    }

}

Thank you!

    
asked by Mar Aq 20.10.2017 в 02:41
source

1 answer

0

Direct communication between fragments is not recommended. A fragment should never know another fragment, let alone take for granted its existence. In any case, the fragment A sends the image to the MainActivity and the MainActivity sends it to the fragmentB. This can be achieved in the following way:

1- You create a communication interface between the fragment and the activity, which must be implemented by the latter. Assign this interface in the onAttach of the fragment.

public static class FragmentoA extends Fragment {
    Callback listener;

    ....

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            // Establece la actividad como objeto listener.
            listener = (Callback) activity;
        } catch (ClassCastException e) {
            // La actividad no implementa la interfaz.
            throw new ClassCastException(activity.toString() + 
                " debe implementar la interfaz Callback del fragmento");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }

    ...
    // La actividad que contiene el fragmento debe implementar esta interfaz.
    public interface Callback {
        public void onEnvioImagen(Bitmap imagen);
    }
    ...
}

2- In this same fragment, When you receive the image , call the interface method that will cause the MainActivity to take over:

listener.onEnvioImagen(tuImagen);

3- Implements the Callback interface in the activity and overwrites the method:

public class MainActivity extends AppCompatActivity implements FragmentoA.Callback  {
 public void onEnvioImagen(Bitmap imagen){
    //Codigo para crear el nuevo fragmento y envíale la imagen
    FragmentUtils.replaceFragment(getSupportFragmentManager(), R.id.fragmentLayout_de_main,
                    FragmentoB.newInstance(imagen), TAG_MAIN_FRAGMENT);
 }
}
    
answered by 20.10.2017 в 14:45