Skip to content

Instantly share code, notes, and snippets.

@root-ansh
Created June 3, 2019 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save root-ansh/f4a3a4a35829d84b04479a88fc9bb7bf to your computer and use it in GitHub Desktop.
Save root-ansh/f4a3a4a35829d84b04479a88fc9bb7bf to your computer and use it in GitHub Desktop.
Camera basics : delegating to system camera api
public class MainActivity extends AppCompatActivity {
private static final int RQ_CAPTURE_IMG_CODE_THUMB = 100;
ImageView ivTemp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivTemp = findViewById(R.id.iv_tmpcheck);
Button btCallCameraThumbnail = findViewById(R.id.bt_call_camera_thumbnail);
btCallCameraThumbnail.setOnClickListener(v -> callCameraForThumbnail());
}
private void callCameraForThumbnail() {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (i.resolveActivity(getPackageManager()) != null) {
startActivityForResult(i, RQ_CAPTURE_IMG_CODE_THUMB);
}
// ^--Ensures that there's a camera activity to handle the intent
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RQ_CAPTURE_IMG_CODE_THUMB && resultCode == RESULT_OK) {
if (data != null && data.getExtras() != null) {
Bitmap imageBitmap = (Bitmap) data.getExtras().get("data");
onBitmapRecieved(imageBitmap);
}
}
}
private void onBitmapRecieved(Bitmap imageBitmap) {
ivTemp.setImageBitmap(imageBitmap);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment