Android calls system camera get return data

Source: Internet
Author: User

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

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.