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: