Android call system camera

Source: Internet
Author: User

When developing Android applications, we often need cameras for taking photos or recording videos. Here we will introduce the simplest method: Call the System camera to take photos and save the photos.

How to start the camera:

startActivityForResult(new Intent("android.media.action.IMAGE_CAPTURE"), TAKE_PICTURE);

We only need to override the method for receiving parameters after successful shooting.

Protected void onactivityresult (INT requestcode, int resultcode, intent data) Method

For example:

final int TAKE_PICTURE = 1;protected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == TAKE_PICTURE) {if (resultCode == RESULT_OK) {Bitmap bm = (Bitmap) data.getExtras().get("data");imgPhoto.setImageBitmap(bm);// ??¨ª?????¨º??¨²ImageView¨º¨¨ª?¨¦?¡ê?private// ¡ä?¡ä|?D??¡ä?¡ä¡é?¡§¨º?¡¤?¡ä??¨²sdCardExit = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);// ImageView img;if (sdCardExit) {myRecAudioDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/custom_picture");if (!myRecAudioDir.exists()) {myRecAudioDir.mkdir();}} else {mMakeTextToast("¡ä?¡ä¡é?¡§2?¡ä??¨²", true);}try {File f = File.createTempFile(strTempFile, ".jpg",myRecAudioDir);BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));photoPath=f.getPath();/* 2¨¦¨??1??¡Áa¦Ì¦Ì¡¤?¡¤¡§ */bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);/* ¦Ì¡Â¨?flush()¡¤?¡¤¡§¡ê??¨¹D?BufferStream */bos.flush();/* ?¨¢¨º?OutputStream */bos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}}
The shooting effect is as follows:
 
The returned results are as follows:
 
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.