Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this questionI have the requirement that if the user clicks the 'take picture' now button, capt开发者_如何学运维ure the user's image using the camera and display it to the user.
Can anybody provide sample code?Begin by creating an Intent of within your button click handler:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Intents are often started using the startActivity() method. However, in this case, your application wants to wait for, and use, the results of the Camera image capture. Therefore, you want to send the Intent using the startActivityForResult() call instead. This way you can inspect the results and use the captured image.
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
When the startActivityForResult() method is called, the Activity is launched. Once that Activity finishes, the calling Activity is presented with a result within its onActivityResult() handler. Therefore, you need to implement the onActivityResult() callback method within your application’s Activity as follows:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
// do something
}
}
Yes, there it is again: CAMERA_PIC_REQUEST. This is a value that you need to define within your application as the request code returned by the Camera image capture Intent, like so:
private static final int CAMERA_PIC_REQUEST = 1337;
The image that is returned from this is appropriate for display on a small device screen. It comes in directly to the results as an Android Bitmap object:
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
What you do with the Bitmap object is up to you. Displaying it on screen is as easy as calling the setImageBitmap() method on an ImageView.
精彩评论