Android calls system camera to take pictures and save on Local

Source: Internet
Author: User

Import Java.io.file;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.util.calendar;import Java.util.locale;import Android.annotation.SuppressLint; Import Android.app.activity;import android.content.intent;import android.graphics.bitmap;import android.os.Bundle; Import Android.os.environment;import Android.provider.mediastore;import Android.text.format.dateformat;import Android.util.log;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; Import Android.widget.imageview;import android.widget.Toast; @SuppressLint ("Sdcardpath") public class Mainactivity Extends activity {/** Called when the activity is first created. */private Button button;private ImageView view; @Overridep ublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); button = (button) Findviewbyid (R.id.button1); view= (ImageView) Findviewbyid (r.id.imageview1 ); Button.setOnclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubintent int ent = new Intent (mediastore.action_image_capture); Startactivityforresult (Intent, 1);});} @SuppressLint ("Sdcardpath") @Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {// TODO auto-generated Method Stubsuper.onactivityresult (Requestcode, ResultCode, data); if (ResultCode = = Activity.result _OK) {String sdstatus = environment.getexternalstoragestate (); if (!sdstatus.equals (environment.media_mounted)) {// Detects if SD is available log.i ("Testfile", "SD card is not avaiable/writeable right now."); return;} New DateFormat (); String name = Dateformat.format ("Yyyymmdd_hhmmss", Calendar.getinstance (Locale.china)) + ". jpg"; Toast.maketext (this, name, Toast.length_long). Show (); Bundle bundle = Data.getextras ();   Bitmap Bitmap = (Bitmap) bundle.get ("Data");//Get the data returned by the camera and convert it to Bitmap picture format FileOutputStream b = null; File File = new file ("/sdcard/image/"); File.mkdirs ();//Create TextClip string fileName = "/sdcard/image/" +name;try {b = new FileOutputStream (fileName); Bitmap.compress ( Bitmap.CompressFormat.JPEG, b);//write data to file} catch (FileNotFoundException e) {e.printstacktrace ();} finally {try { B.flush (); B.close ();} catch (IOException e) {e.printstacktrace ();}} Try{view.setimagebitmap (bitmap);//Display the picture in ImageView}catch (Exception e) {log.e ("error", E.getmessage ());}}}

XML file

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ".        Mainactivity "> <imageview android:id=" @+id/imageview1 "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_alignparentleft= "true" android:src= "@drawable/ic_launche R "/> <button android:id=" @+id/button1 "android:layout_width=" Wrap_content "Android:layout_h eight= "Wrap_content" android:layout_alignparentbottom= "true" android:layout_centerhorizontal= "true" a Ndroid:layout_marginbottom= "15DP" Android:text= "button"/></relativelayout> 

Finally, remember to add permissions

<!--Create and delete files in SDcard permissions--
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!--write data to sdcard permissions--
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name = "Android.hardware.camera"/>
<uses-feature android:name = "Android.hardware.camera.autofocus"/>


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.