Android calls the camera and stores the photo on the SD card.

Source: Internet
Author: User

In Android, there are two ways to take photos. One is to call the camera that comes with the system and then use the photo data it returns. Another method is to use the Camera class and other related classes to implement Camera functions. This method has a relatively high system and is complicated for dyeing. Generally, you only need to use the first method for common applications.
Code for starting a camera with Intent: Copy codeThe Code is as follows: Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE );
StartActivityForResult (intent, 1); after taking a picture, you can get the Bitmap object in onActivityResult (int requestCode, int resultCode, Intent data. Bitmap bitmap = (Bitmap) data. getExtras (). get ("data ");

Before storing images to the SD card, check whether the SD card is available.Copy codeThe Code is as follows: String sdStatus = Environment. getExternalStorageState ();
If (! SdStatus. equals (Environment. MEDIA_MOUNTED) {// check whether sd is available
Log. v ("TestFile ",
"SD card is not avaiable/writeable right now .");
Return;
}

Run the following code to save the image file to the "sdcard/myImage/images" folder named "2017111.jpg"Copy codeThe Code is as follows: File file = new File ("/sdcard/myImage /");
File. mkdirs (); // create a folder
String fileName = "/sdcard/myImage/111.jpg ";
Try {
B = new FileOutputStream (fileName );
Bitmap. compress (Bitmap. CompressFormat. JPEG, 100, B); // write data to a file
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Finally {
Try {
B. flush ();
B. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}

In addition, you must first configure the permission in the Mainifest. xml file to read and write the SD card file:Copy codeThe Code is 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 photo, display it on the screen, and save it to the SD card.
The complete code is as follows::
MyCaremaActivity. javaCopy codeThe Code is 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) {// check whether sd is available
Log. v ("TestFile ",
"SD card is not avaiable/writeable right now .");
Return;
}
Bundle bundle = data. getExtras ();
Bitmap bitmap = (Bitmap) bundle. get ("data"); // obtain the data returned by the camera and convert it to the Bitmap image format.
FileOutputStream B = null;
File file = new File ("/sdcard/myImage /");
File. mkdirs (); // create a folder
String fileName = "/sdcard/myImage/111.jpg ";
Try {
B = new FileOutputStream (fileName );
Bitmap. compress (Bitmap. CompressFormat. JPEG, 100, B); // write data to a file
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Finally {
Try {
B. flush ();
B. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
(ImageView) findViewById (R. id. imageView). setImageBitmap (bitmap); // display the image in ImageView
}
}
}

Main. xmlCopy codeThe Code is 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 to start the camera"/>
<ImageView
Android: id = "@ + id/imageView"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "#999999"/>
</LinearLayout>

AndroidMainifest. xmlCopy codeThe Code is 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" type = "codeph" text = "/codeph">
<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"
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>

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.