0

My app needs a profile's image either taking a photo or choose from gallery. I'm on a fragment and when I click a button I can choose if take a photo or choose it from gallery. The function works and I open the camera and the gallery, but when I return on my fragment I don't see the image taking or choosing into my ImageView why?

I use this code:

AlertDialog to choose the option:

final CharSequence[] items = { "Take Photo", "Choose from Library", "Cancel" };
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Add Photo");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        if (items[item].equals("Take Photo")) {
                            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                            startActivityForResult(intent, REQUEST_CAMERA);
                        } else if (items[item].equals("Choose from Library")) {
                            Intent intent = new Intent();
                            intent.setType("image/*");
                            intent.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
                        } else if (items[item].equals("Cancel")) {
                            dialog.dismiss();
                        }
                    }
                });
                builder.show();

Taking photo:

Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
 Toast.makeText(getActivity().getApplicationContext(),"OK" , Toast.LENGTH_LONG).show();
etImage.setImageBitmap(thumbnail);

Choose from gallery:

Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
etImage.setImageURI(selectedImageUri);           

The whole onActivityResult() is

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == getActivity().RESULT_OK) {
            if (requestCode == REQUEST_CAMERA) {
                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                Toast.makeText(getActivity().getApplicationContext(),"OK" , Toast.LENGTH_LONG).show();
                etImage.setImageBitmap(thumbnail);
            } else if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                etImage.setImageURI(selectedImageUri);                
        }

    }         

So what is the problem? Logcat doesn't show me any errors

Shaila
  • 1
  • 3

0 Answers0