Android calls the camera and stores the photos on the SD card to implement the method _android

Source: Internet
Author: User
Tags flush xmlns
There are two ways to implement a photo in Android, one is to call the system's own camera and then use the photo data it returns. Another one is to use the camera class and other related classes to achieve the camera function, this method of the system is relatively high, dyeing is also more complex, the general common application just use the first one can.
start the camera code with intent
Copy Code code as follows:

Intent Intent = new Intent (mediastore.action_image_capture);
Startactivityforresult (Intent, 1), after the photo can be onactivityresult (int requestcode, int resultcode, intent data) Gets the bitmap object in the. Bitmap Bitmap = (Bitmap) Data.getextras (). Get ("data");

It is best to check that the SD card is available before storing the image in the SD card.
Copy Code code as follows:

String sdstatus = Environment.getexternalstoragestate ();
if (!sdstatus.equals (environment.media_mounted)) {//detect SD is available
LOG.V ("Testfile",
"SD card isn't avaiable/writeable right now");
Return
}

The following code enables you to save an image file under the "sdcard/myimage/" folder, with the name "111.jpg"
Copy Code code as follows:

File File = new file ("/sdcard/myimage/");
File.mkdirs ();//Create Folder
String fileName = "/sdcard/myimage/111.jpg";
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 ();
}
}

Also note that the read-write SD card file must first configure permissions in the Mainifest.xml file:
Copy Code code as follows:

<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

A demo that calls the system camera to take a picture, display it on the screen, and save it to an SD card.
The complete code is as follows
Mycaremaactivity.java
Copy Code code as follows:

Package barry.android.c;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
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.util.Log;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.ImageView;
public class Mycaremaactivity extends activity {
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Button button = (button) Findviewbyid (R.id.button);
Button.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Intent Intent = new Intent (mediastore.action_image_capture);
Startactivityforresult (Intent, 1);
}
});
}
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
if (ResultCode = = ACTIVITY.RESULT_OK) {
String sdstatus = Environment.getexternalstoragestate ();
if (!sdstatus.equals (environment.media_mounted)) {//detect SD is available
LOG.V ("Testfile",
"SD card isn't avaiable/writeable right now");
Return
}
Bundle Bundle = Data.getextras ();
Bitmap Bitmap = (Bitmap) bundle.get ("Data")//Get the data returned by the camera and convert to Bitmap picture format
FileOutputStream B = null;
File File = new file ("/sdcard/myimage/");
File.mkdirs ();//Create Folder
String fileName = "/sdcard/myimage/111.jpg";
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 ();
}
}
((ImageView) Findviewbyid (R.id.imageview)). Setimagebitmap (bitmap);//show the picture in ImageView
}
}
}

Main.xml
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >
<button
Android:id= "@+id/button"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Click Start Camera"/>
<imageview
Android:id= "@+id/imageview"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:background= "#999999"/>
</LinearLayout>

Androidmainifest.xml
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns: Android= "http://schemas.android.com/apk/res/android"
package= "BARRY.ANDROID.C"
android:versioncode= "1"
Android:versionname= "1.0" >
<uses-sdk android:minsdkversion= "7"/>
<uses-permission android: Name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= " Android.permission.MOUNT_UNMOUNT_FILESYSTEMS "/>
<application
android:icon=" @drawable/ic_launcher " Br>android:label= "@string/app_name" >
<activity
android:label= "@string/app_name"
Android:name=. Mycaremaactivity ">
<intent-filter >
<action android:name=" Android.intent.action.MAIN "/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity >
</application>
</manifest>
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.