Android calls camera and obtains photos

Source: Internet
Author: User

Case R. Id. openphoto:
Intent intent = new intent ();
/* Enable pictures image type to set to image */
Intent. settype ("image /*");
/* Use the intent. action_get_content action */
Intent. setaction (intent. action_get_content );
/* Return to the current screen after obtaining the photo */
Startactivityforresult (intent, 1 );
Request_camera = 0;
Break;
Case R. Id. Camera:
Intent getimagebycamera = new intent (
"Android. Media. Action. image_capture ");
Startactivityforresult (getimagebycamera, request_camera );
Request_camera = 1;
Break;
Default:
Break;
}
}
@ Override
Public Boolean onkeydown (INT keycode, keyevent event ){
// Todo auto-generated method stub
If (keycode = keyevent. keycode_back ){
Startactivity (new intent (imagepageactivity. This, ordermsgactivity. Class ));
Imagepageactivity. This. Finish ();
}
Return super. onkeydown (keycode, event );
}
@ Override
Protected void onactivityresult (INT requestcode, int resultcode, intent data ){
If (request_camera = 0 ){
Uri uri = data. getdata ();
// Log. E ("Uri", Uri. tostring ());
Contentresolver Cr = This. getcontentresolver ();
Try {
Bitmap bitmap = bitmapfactory. decodestream (Cr. openinputstream (URI ));
/* Set bitmap to imageview */
Imageview. setimagebitmap (Bitmap );
} Catch (filenotfoundexception e ){
// Log. E ("exception", E. getmessage (), e );
}
} Else if (request_camera = 1 ){
Bitmap mybitmap = NULL;
Try
{
Super. onactivityresult (requestcode, resultcode, data );
Bundle extras = data. getextras ();
Mybitmap = (Bitmap) extras. Get ("data ");
Bytearrayoutputstream baos = new bytearrayoutputstream ();
Mybitmap. Compress (bitmap. compressformat. JPEG, 100, baos );
Byte [] mcontent = baos. tobytearray ();
} Catch (exception E)
{
E. printstacktrace ();
}
// Bind the obtained image to the control for display
Imageview. setimagebitmap (mybitmap); // convert the photo into a rounded corner and display it in the preview control.
}
Super. onactivityresult (requestcode, resultcode, data );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.