Android calls system camera get return data
Because the project needs to call the camera, implement upload photos, such as Weibo, medium functionality. Android can be very easy to call the system camera, and return bitmap data, but a little insufficient, it returns the bitmap size is very small, the clarity is not enough, this problem will be resolved later. This is demonstrated by the code below.
1. Interface layout
Res/layout defines a simple layout, a button and a imageview that are used to jump system camera activity and display system camera return data.
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6 7 <Button8 Android:id= "@+id/cap"9 Android:layout_width= "Fill_parent"Ten Android:layout_height= "Wrap_content" One Android:text= "Camera" /> A <ImageView - Android:id= "@+id/image" - Android:layout_width= "Fill_parent" the Android:layout_height= "Fill_parent" - /> - </Linea
2.Intent Realization Jump System camera
Intent intent=New1);
3. Overwrite the Onactivityresult method
This method is used to receive the system camera return data and display the picture
1 @Override2 protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {3 if(requestcode==1){4 if(resultcode==RESULT_OK) {5Bitmap bitmap= (Bitmap) Data.getextras (). Get ("Data"));6 Image.setimagebitmap (bitmap);7SimpleDateFormat sdf=NewSimpleDateFormat ("Yyyymmddhhmmsssss");8String dir=environment.getexternalstoragedirectory (). GetAbsolutePath () +9File.separator + "Cap";TenString Path=dir+file.separator+sdf.format (NewDate ()) + ". JPEG "; OneFile directionary=NewFile (dir); A if(!directionary.exists ()) { - Directionary.mkdir (); - } theFile pic=NewFile (path); -FileOutputStream fos=NULL; - Try { -fos=Newfileoutputstream (pic); +Sendbroadcast (NewIntent (Intent.action_media_scanner_scan_file, Uri.parse ("file://" +path )); -Bitmap.compress (compressformat.jpeg,100, FOS); +Toast.maketext (Getapplicationcontext (), "photo generation \ n" +path, Toast.length_long). Show (); A}Catch(FileNotFoundException e) { at - e.printstacktrace (); -}finally{ - if(fos!=NULL){ - Try { - fos.close (); in}Catch(IOException e) { - //TODO auto-generated Catch block to e.printstacktrace (); + } - } the } * $ }Panax Notoginseng } - Super. Onactivityresult (Requestcode, ResultCode, data); the}
Android calls system camera get return data