Search code examples
androidandroid-imageview

Android ImageView can't get image from gallery or camera


I want to pick a photo from gallery or camera on Android and show the photo on ImageView, open gallery or camera was fine and there's no problem with permission. But the problem is, the picture that I select or I take from the camera was not shown on ImageView, and there's no error.

Below is my code

private static final int CAMERA_REQUEST_CODE = 1;
private static final int SELECT_FILE = 0;

public PersonalDataStep(){
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.personal_data_step, container, false);
    ButterKnife.bind(this, view);

    //imageEktp.setClickable(true);

    getPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Log.d("test : ", "testing");
            CharSequence menu[] = new CharSequence[]{"Take From Galery", "Open Camera"};
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setTitle("Pick a Picture");
            builder.setItems(menu, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    if(i == 0){
                        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        intent.setType("image/*");
                        startActivityForResult(intent, SELECT_FILE);
                    }else if(i == 1){
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(intent, CAMERA_REQUEST_CODE);

                    }
                }
            });
            builder.show();
        }
    });

    return view;
}

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

    if(resultCode== Activity.RESULT_OK) {
        if (resultCode == CAMERA_REQUEST_CODE) {
            Bundle bundle = data.getExtras();
            final Bitmap bitmap = (Bitmap) bundle.get("data");
            imageEktp.setImageBitmap(bitmap);
        } else if (resultCode == SELECT_FILE) {
            Uri selectedImage = data.getData();
            imageEktp.setImageURI(selectedImage);
            Log.d("image from galery : ", selectedImage.toString());
        }
    }else{
        Toast.makeText(getActivity(), "You haven't picked Image",Toast.LENGTH_LONG).show();
    }
}

Solution

  • Small typo in your code

    if (resultCode == CAMERA_REQUEST_CODE) {
     ...
    } else if (resultCode == SELECT_FILE) {
     ...
    }
    

    Should be

    if (requestCode == CAMERA_REQUEST_CODE) {
     ...
    } else if (requestCode == SELECT_FILE) {
     ...
    }