Android call system camera to take photos of two methods to achieve the example _android

Source: Internet
Author: User

Android Call system camera take photos of two ways to achieve the example

In our Android development, we often need to do this function, call the system camera to take pictures, and then get the photos taken. Here are the two methods I've summarized to get the photos after the shot, one is through the bundle to get the compressed photos, one is through the SD card to obtain the original image.

Here is the demo code:

Layout file:

<linearlayout 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:orientation= "vertical" 
  android:gravity= "Center_horizontal"  
  > 
 
  <button 
    Android:id= "@+id/button1" 
    android:layout_width= "wrap_content" 
    android:layout_height= "Wrap_content" 
    android:text= "Take photos get thumbnails"/> 
 
  <button 
    android:id= @+id/button2 " 
    android:layout_width=" Wrap_content " 
    android:layout_height=" wrap_content " 
    android:layout_margintop=" 14DP " 
    android:text = "Get the original picture"/> 
 
  <imageview 
    android:id= "@+id/imageview1" 
    android:layout_width= "Match_parent" 
    android:layout_height= "match_parent" 
    android:src= "@drawable/ic_launcher"/> 
 
</ Linearlayout> 

Java code:



Package Com.example.cameardemo; 
Import Java.io.File; 
Import Java.io.FileInputStream; 
Import java.io.FileNotFoundException; 
 
Import java.io.IOException; 
Import android.content.Intent; 
Import Android.graphics.Bitmap; 
Import Android.graphics.BitmapFactory; 
Import Android.net.Uri; 
Import Android.os.Bundle; 
Import android.os.Environment; 
Import Android.provider.MediaStore; 
Import android.support.v7.app.ActionBarActivity; 
Import Android.util.Log; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.widget.Button; 
 
Import Android.widget.ImageView;  public class Mainactivity extends Actionbaractivity implements Onclicklistener {private static int request_thumbnail 
  = 1;//request thumbnail signal identification private static int request_original = 2;//Request Original signal identification Private Button button1, Button2; 
  Private ImageView Mimageview; private string SDPATH;//SD card path private string picpath;//picture storage path @Override protected void OnCreate (Bundle Savedin StancestaTE) {super.oncreate (savedinstancestate); 
 
    Setcontentview (R.layout.fragment_main); 
    Button1 = (Button) Findviewbyid (R.id.button1); 
    Button2 = (Button) Findviewbyid (R.id.button2); 
 
    Mimageview = (ImageView) Findviewbyid (R.ID.IMAGEVIEW1); 
    Button1.setonclicklistener (this); 
    Button2.setonclicklistener (this); 
    Gets the path of the SD card Sdpath = Environment.getexternalstoragedirectory (). GetPath (); 
    Picpath = Sdpath + "/" + "temp.png"; 
  LOG.E ("SdPath1", Sdpath); 
      @Override public void OnClick (view view) {switch (View.getid ()) {case r.id.button1://The first method, get the compression graph 
      Intent intent1 = new Intent (mediastore.action_image_capture); 
      Start Camera Startactivityforresult (Intent1, Request_thumbnail); 
 
    Break 
      Case r.id.button2://The second method, get the original Intent Intent2 = new Intent (mediastore.action_image_capture); 
      Uri uri = uri.fromfile (new File (Picpath)); Specifies a stored path Intent2.putextra for the photographed picture (MEDIASTORE.EXTRA_OUTPUT, URI); 
      Startactivityforresult (Intent2, request_original); 
 
    Break 
    Default:break; }/** * Returns the callback method when applied */@Override protected void Onactivityresult (int requestcode, int resultcode, in 
 
    Tent data) {Super.onactivityresult (Requestcode, ResultCode, data); if (ResultCode = = RESULT_OK) {if (Requestcode = = Request_thumbnail) {//corresponding to the first method/** * Take out this method The photo will be compressed by default, because if the camera's pixel is higher, the picture will be more high-definition, * if the graph too is caused by the memory overflow (OOM), so this method will default to the image compression/Bundle Bundle = data. 
        Getextras (); 
        Bitmap Bitmap = (Bitmap) bundle.get ("Data"); 
      Mimageview.setimagebitmap (bitmap); 
         else if (ResultCode = = request_original) {//corresponding to the second method/** * This method is to read the image through the path of the memory card, so the picture is taken 
        * * FileInputStream FIS = null; 
          try {log.e ("sdPath2", Picpath); 
          Converts a picture into a byte stream fis = new FileInputStream (Picpath); 
        Convert a stream to a picture  Bitmap Bitmap = Bitmapfactory.decodestream (FIS); 
        Mimageview.setimagebitmap (bitmap); 
        catch (FileNotFoundException e) {e.printstacktrace (); }finally{try {fis.close ();//Close Stream} catch (IOException e) {e.printstacktr 
          Ace (); 

 } 
        } 
      } 
    } 
  } 
 
}

Finally, do not forget to add the permission to read the SD card on the manifest file:



<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" Android "package=" Com.example.cameardemo "android:versioncode=" 1 "android:versionname=" 1.0 "> &LT;USES-SD K android:minsdkversion= "8" android:targetsdkversion= "/> <!--the right to operate an SD card--> <uses-permissio 
    n android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowbackup= "true" 
    android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > 
      <activity android:name= "com.example.cameardemo.MainActivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category Andro 
      Id:name= "Android.intent.category.LAUNCHER"/> </intent-filter> <!--with camera function--> <intent-filter > &Lt;action android:name= "Android.media.action.IMAGE_CAPTURE"/> <category android:name= "Android.int" Ent.category.DEFAULT "/> </intent-filter> </activity> </application> </manifest& 

 Gt

  Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.