Pages

Thursday, June 26, 2014

Capture image from camera in android


To capture image from android first fire an intent.
       
        private static final int REQUEST_CODE_IMAGE = 101;

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, REQUEST_CODE_IMAGE);


//  get data on on Activity Result use the below code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
 if (requestCode == REQUEST_CODE_IMAGE && resultCode == RESULT_OK) {

   if (data != null) {
    if (data.getExtras().get("data") != null) {
 
     Uri fileUri = data.getData();
     String filePath = getRealPathFromURI(fileUri,
       MediaStore.Images.Media.DATA);
     File file = new File(filePath);
     String fileName = file.getName();


     try {
      Bitmap photo = (Bitmap) data.getExtras().get("data");
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      photo.compress(Bitmap.CompressFormat.PNG, 100, stream);



     } catch (Exception ex) {

     }
    }
   }
  }

No comments:

Post a Comment