Learning Android can not learn the camera, now a variety of beauty cameras, friends Circle hair Map, now upgrade directly after the drop will be able to take photos, the source of all kinds of photos is also a camera, pull away, a bit evil, or simply learn the use of the camera in Android, The use of the camera in Android is generally divided into two using the existing camera app (can be understood as the system built-in camera) and create a separate camera app, the first way to be easy to understand, the second way if you can do a good job to support a company ~ Today, simply describe the first kind of call ~
Basic Work
Call the camera need to take photos, photography needs to be stored, there are external storage needs to set permissions, the settings of the three permissions: one is to call camera permissions, the second is to require the device has a camera, the third is the permission to write on the external device:
<uses-permission android:name= "Android.permission.CAMERA"/> <uses-feature android:name= " Android.hardware.camera "/> <uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/ >
A simple page, a buttom, a imageview:
Demo Implementation
Simple trilogy, first create intent, then start intent, finally receive results, no front camera, use is analog:
Photo event:
public void cameraevent (view view) {intent = new intent (mediastore.action_image_capture);//Create an intent to get the picture file= Getoutputmediafile (); Get Path Intent.putextra (mediastore.extra_output, Uri.fromfile (file)); Set file name//start Intentstartactivityforresult (Intent, image_request_code);}
Ways to get the file path:
Picture path private static file Getoutputmediafile () {File Mediastoragedir = new file ( Environment.getexternalstoragepublicdirectory (environment.directory_pictures), tag); if (!mediaStorageDir.exists ( ) {if (!mediastoragedir.mkdirs ()) {LOG.D (tag, "Storage Directory Creation failed"); return null;}} Create file name string TimeStamp = new SimpleDateFormat ("Yyyymmdd_hhmmss"). Format (new Date ()); File MediaFile = Null;mediafile = new file (Mediastoragedir.getpath () + file.separator+ "Img_" + TimeStamp + ". jpg"); LOG.D (tag, "Storage directory:" + mediafile); return mediafile;}
Receiving events after completion requires rewriting the Onactivityresult method in the activity:
@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO auto-generated method stub if (Requestcode = = IMAGE_REQUEST_CODE&&RESULTCODE==RESULT_OK) {imageview= (ImageView) Findviewbyid ( R.ID.CAMERA_IMG)/////Two settings//pictures can use this bitmap bitmap=bitmapfactory.decodefile (file.tostring ()) When the picture is large; Imageview.setimagebitmap (bitmap);// Imageview.setimageuri (uri.fromfile (file));} Super.onactivityresult (Requestcode, ResultCode, data);}
The effect is as follows:
Main reference : http://developer.android.com/guide/topics/media/camera.html
Android Hardware Learning-camera