Created
June 3, 2019 00:05
-
-
Save root-ansh/f4a3a4a35829d84b04479a88fc9bb7bf to your computer and use it in GitHub Desktop.
Camera basics : delegating to system camera api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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