Android calls the camera to take a photo and display it to the ImageView control

Source: Internet
Author: User

In one of the previous articles, we introduced a simple camera opening function, as described in. Android Simple call camera function, to realize the open camera function, this time will be taken in front of the photo display into the ImageView, forming a complete effect to see the example Mainactivity.java
Package Com.example.camera;import Java.io.file;import Android.app.activity;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.util.log;import Android.view.View; Import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imageview;public class Mainactivity extends activity {/** Called when the activity is first created. */string Sd_card_temp_dir; Bitmap mybitmap;private ImageView ImageView; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main);//sd_card_temp_dir = Environment.getexternalstoragedirectory () + File.separator + "tmp.jpg";//Set the file name saved after the photo, similar to the cache file ImageView = (ImageView) Findviewbyid (R.ID.IMAGEVIEW1); Button btn = (button) Findviewbyid (R.id.button1); Btn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated method Stubintent cameraintent = new Intent (mediastore.action_imag E_capture); Cameraintent.putextra (Android.provider.mediastore.extra_output,uri.fromfile (New File (SD_CARD_TEMP_ DIR)); Startactivityforresult (cameraintent, 0);});} @Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO auto-generated method stub Super.onactivityresult (Requestcode, ResultCode, data), if (Requestcode = = 0) {log.d ("Requestcode", "Need 0"); if ( ResultCode = = RESULT_OK) {log.d ("ResultCode", "OK!!!" + sd_card_temp_dir); Mybitmap = Bitmapfactory.decodefile (SD_CARD_ Temp_dir); Imageview.setimagebitmap (Mybitmap);} ELSE{LOG.D ("ResultCode", "" "+ ResultCode);}} ELSE{LOG.D ("Requestcode", "not Need");}}}
Have a look? activity_main.xml
<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 "Tools:context =". Mainactivity "><buttonandroid:id=" @+id/button1 "android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:layout_alignparentbottom=" true "android:layout_alignparentright=" true "android:text=" button "/><imageviewandroid:id=" @+id/imageview1 "android:layout_width=" wrap_content "android:layout_height=" Wrap_ Content "Android:layout_above=" @+id/button1 "android:layout_alignparentleft=" true "Android:layout_ Alignparentright= "true" android:layout_alignparenttop= "true" android:src= "@drawable/ic_launcher"/></ Relativelayout>
It's best to configure the permissions again. See Androidmainfest.xml
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.camera "android:versioncode=" 1 "android:versionname=" 1.0 "><uses-permission Android:name= "Android.permission.CAMERA"/><uses-feature android:name= "Android.hardware.camera"/>< Uses-feature android:name= "Android.hardware.camera.autofocus"/><uses-permission android:name= " Android.permission.WRITE_EXTERNAL_STORAGE "></uses-permission><uses-sdkandroid:minsdkversion=" 8 " android:targetsdkversion= "/><uses-permission" android:name= "Android.permission.READ_EXTERNAL_STORAGE"/ ><applicationandroid:allowbackup= "true" android:hardwareaccelerated= "false" android:icon= "@drawable/ic_ Launcher "android:label=" @string/app_name "android:theme=" @style/apptheme "><activityandroid:name=" Com.example.camera.MainActivity "android:label=" @string/app_name "><intent-filter><action android: Name= "Android.intent.action.MAIN" /><category android:name= "Android.intent.category.LAUNCHER"/></intent-filter></activity> </application></manifest>
This place, also pay attention to IS? android:hardwareaccelerated= "false"? This sentence, suppose we do not add this sentence to the case. will be very easy the following problem arises bitmap too large to being uploaded into a texture so. We need to turn off the hardware acceleration feature.

Android calls the camera to take a photo and display it to the ImageView control

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.