Android multimedia-System camera
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 );}}}}