-1

I have a fragment which contains four images each image when clicked it opens a gallery for the user to select image from there in the activity result when retrieving the image name and path I'm passing this result to an activity through intent that handles the cropping of the image in the cropping activity after the cropping is done i don't know how to send back the result of the cropped image to the fragment and place it in the image view same process is done for the 4 images how can i achieve that. Here is my starting code.

enter code here

attach_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pickFromGallery();
        }
    });
  private void pickFromGallery()
  {
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    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 = getContext().getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            // Move to first row
            assert cursor != null;
            cursor.moveToFirst();

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


        } else {
            Toast.makeText(getActivity(), "You haven't picked Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_LONG)
                .show();
    }
}
private void startCrop(String imageUri)
{
    Intent intent = new Intent(getActivity(),CropActivity.class);
    intent.putExtra("Image",imageUri);
    startActivity(intent);
}

} in my cropping activity here is my code

enter code here
Intent i = getIntent();
    image = i.getStringExtra("Image");
    Log.d("Image",""+image);
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    int imageWidth = (int) ( (float) metrics.widthPixels / 1.5 );
    int imageHeight = (int) ( (float) metrics.heightPixels / 1.5 );
    bitmap = BitmapLoadUtils.decode(image, imageWidth, imageHeight);
    imageCropView.setImageBitmap(bitmap);
    imageCropView.setAspectRatio(3, 2);
    findViewById(R.id.crop_btn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            if(!imageCropView.isChangingScale())
            {
                Bitmap b = imageCropView.getCroppedImage();
            }
        }
    });

after cropping here the image i need to set this cropped image in the image view in the fragment same for the rest of the image with the same process how can this be achieved . Any help would be appreciated.

Mostafa Addam
  • 6,956
  • 4
  • 20
  • 38

2 Answers2

1

You can start the activity using startActivityForResult(...) method, so you can send data back.
As for how to pass the bitmap using intent, check this stackoverflow question.

Then you can get the Bitmap on the onActivityResult(...) method of your fragment.

Community
  • 1
  • 1
Rick Sanchez
  • 4,528
  • 2
  • 27
  • 53
  • if i put startActivity for result in my crop activity and through intent the bitmap in the fragment activity i handle the intent how can i know what image is selected in that way there are 4 images can u elaborate more please – Mostafa Addam Oct 08 '15 at 15:05
  • You put startActivityForResult in your fragment, and call then call setResult from CropActivity, and pass the bitmap (or the path of the saved bitmap) via intent. You can setUp an int when an image is clicked, based on the image, and then onActivityResult, check which image was clicked based on the saved int value – Rick Sanchez Oct 08 '15 at 15:24
1

Passing bitmaps via intents is not a good practice and will not work if the bitmap is large and therefore should be avoided.

I would suggest u to save the bitmap on disk and pass the path of the saved image to the fragment. Check the edit of the accepted answer in this post

Also shouldn't call startActivity() from a fragment. It should be done from the another activity. Check this Communicating With Fragments Guide

Community
  • 1
  • 1
vishnus
  • 728
  • 2
  • 7
  • 20