Android camera intent using (StartActivityForResult)

private static final int CAMERA_CODE = 0;

// This method is used to call the camera activity.
private void cameraIntent(){
  final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getFile(this)) ); 
  startActivityForResult(intent, CAMERA_CODE);
}


//create a temporary file in sd card path.

private File getFile(Context context){
  final File path = new File( Environment.getExternalStorageDirectory(), context.getPackageName() );
  if(!path.exists()){
    path.mkdir();
  }
  return new File(path, "image.tmp");
}


//This method is used to getresult from another activity.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == CAMERA_CODE) {
        final File file = getFile(this);
        try {
          Bitmap captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
          // do whatever you want with the bitmap (Resize, Rename, Add To Gallery, etc)
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
  }
}

Comments

Popular posts from this blog

Input filter (space not allowed) for EditText

Android Versions details

English - Formal and Informal words