Android multimedia-learn to use the system camera in step 5

Source: Internet
Author: User

Android multimedia-learn to use the system camera in step 5

To take a photo with a system camera and return the photo, follow these steps:
[1] Permission

 
   
   
  

[2] Start the camera with intent

Intent intent = new Intent (); intent. setAction (MediaStore. ACTION_IMAGE_CAPTURE); // implicitly start the System camera

[3] set the image storage location and name

File = new File (Environment. getExternalStorageDirectory (), System. currentTimeMillis () +. jpg); // create a new location for saving the camera image. The image name is try {file. createNewFile ();} catch (IOException e) {e. printStackTrace ();} // inform the camera of the position where the image is saved intent. putExtra (MediaStore. EXTRA_OUTPUT, Uri. fromFile (file ));

[4] Start the camera

 startActivityForResult(intent, 0x23);

[5] Get a photo
Set the photo on the ImageView

@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); // data contains the thumbnail if (requestCode = 0x23) {if (resultCode = RESULT_ OK) {imageView. setImageBitmap (BitmapFactory. decodeFile (file. getAbsolutePath (); // imageView. setImageURI (Uri. fromFile (file); // get image }}}
Program source code
Public class MainActivity extends Activity {private Button button; private ImageView imageView; private File file; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) findViewById (R. id. button); imageView = (ImageView) findViewById (R. id. imageview); button. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (); intent. setAction (MediaStore. ACTION_IMAGE_CAPTURE); // implicitly start the System camera file = new File (Environment. getExternalStorageDirectory (), System. currentTimeMillis () +. jpg); // create a new location for saving the camera image. The image name is try {file. createNewFile ();} catch (IOException e) {e. printStackTrace ();} // inform the camera of the position where the image is saved intent. putExtra (MediaStore. EXTRA_OUTPUT, Uri. fromFile (file); startActivityForResult (intent, 0x23) ;}}) ;}@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); // data thumbnail if (requestCode = 0x23) {if (resultCode = RESULT_ OK) {imageView. setImageBitmap (BitmapFactory. decodeFile (file. getAbsolutePath (); // imageView. setImageURI (Uri. fromFile (file); // obtain the image data. getIntExtra (MediaStore. EXTRA_OUTPUT, 0 );}}}}

 

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.