0

I have created a fragment which a button and an image view in it. on button click the camera opens when I take a picture then I want that picture to appear in the imageView.

how can I activate this task?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View rootview = inflater.inflate(R.layout.fragment_new_visitor, container, false);

    capture=rootview.findViewById(R.id.capture);

    capture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            startActivity(intent);
        }
    });


    return rootview;

please help

iBug
  • 35,554
  • 7
  • 89
  • 134
  • 1
    Possible duplicate of https://stackoverflow.com/questions/15408240/take-photo-from-camera-in-fragment – Anmol317 Jan 18 '18 at 12:39
  • 1
    possible duplicate, so many tutorials out there.check this 1) https://stackoverflow.com/questions/5991319/capture-image-from-camera-and-display-in-activity 2) http://droidmentor.com/pick-image-from-gallery-or-camera/ – pavan kvch Jan 18 '18 at 13:39

2 Answers2

0

Just try this one,

Declare this before on create method

private final int requestCode = 20;

inside onclick event, do this

Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(photoCaptureIntent, requestCode);

then override oncreate method

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(this.requestCode == requestCode && resultCode == RESULT_OK){
        Bitmap bitmap = (Bitmap)data.getExtras().get("data");
        imageview.setImageBitmap(bitmap);
    }
}
  • please tell the real use of requestCode=20 – Aditya Dabas Jan 19 '18 at 06:37
  • You can make several calls in a single Activity to startActivityForResult() which allows different Intents to do different actions. Use a request code to identify which is the Intent you are returning from. – Poovarasan Selvaraj Jan 19 '18 at 06:45
  • for example: You can start two activities for result: private static final int req_1 = 1; private static final int req_2 = 2; startActivityForResult(cameraIntent, req_1); startActivityForResult(cameraIntent, req_2); and in your onActivityForResult() @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == req_1 && resultCode == RESULT_OK) { } else if (requestCode == req_2 && resultCode == RESULT_OK) { } } – Poovarasan Selvaraj Jan 19 '18 at 06:53
0

public class BlankFragment extends Fragment {

Button btnGallery,btnCamera;
public static final int GALLEY_REQUEST_CODE = 10;
private static final int CAMERA_REQUEST = 100;
private String TAG = MainActivity.class.getSimpleName();
ImageView image;
private Uri realUri;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_blank, container, false);
}

@Override
public void onStart() {
    super.onStart();

    btnGallery = (Button)getView().findViewById(R.id.btnGallery);
    btnCamera  = (Button)getView().findViewById(R.id.btnCamera);
    image = (ImageView)getView().findViewById(R.id.image);

    btnGallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent openGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(Intent.createChooser(openGallery, "Open Gallery"), GALLEY_REQUEST_CODE);
        }
    });

    btnCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    });


}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == GALLEY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        image.setImageURI(data.getData()); // set image to image view
        try{
            // Get real path to make File
            realUri = Uri.parse(getRealPathFromURI(data.getData()));
            Log.d(TAG,"Image path :- "+realUri);
        }
        catch (Exception e){
            Log.e(TAG,e.getMessage());
        }
    }

    if(requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK){
        if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            image.setImageBitmap(photo);
        }
    }
}
public String getRealPathFromURI(Uri contentUri) {
    String res = null;
    String[] proj = { MediaStore.Images.Media.DATA};
    Cursor cursor = getActivity().getApplicationContext().getContentResolver().query(contentUri, proj, null, null, null);
    assert cursor != null;
    if(cursor.moveToFirst()){;
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        res = cursor.getString(column_index);
    }
    cursor.close();
    return res;
}

}

Ankesh Roy
  • 278
  • 2
  • 8
  • 33