Details about the camera and Image Library instances of the android Calling System

Source: Internet
Author: User

Android mobile phones have their own cameras and libraries. Our projects sometimes use uploading images to the server. Today we have a project that uses this function, so I recorded my code and shared it with you. For the first time I wrote a blog, I hope you will criticize me a lot.
First, the code for calling android album and camera is as follows: Copy codeThe Code is as follows: Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); // call the camera that comes with android
PhotoUri = MediaStore. Images. Media. EXTERNAL_CONTENT_URI;
StartActivityForResult (intent, 1 );

Copy codeThe Code is as follows: Intent I = new Intent (Intent. ACTION_PICK,
Android. provider. MediaStore. Images. Media. EXTERNAL_CONTENT_URI); // calls the android Image Library.
StartActivityForResult (I, 2 );

Copy codeThe Code is as follows: @ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
// Call this method back after photography is completed
Super. onActivityResult (requestCode, resultCode, data );
Case 1:
Switch (resultCode ){
Case Activity. RESULT_ OK: // after taking the photo, click 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/pk4fun /");
File. mkdirs (); // create a folder named pk4fun // name of the photo. In the target folder, use the current time string as the name to ensure that the name of each photo is different. The names of all the photos in other demos that are circulating on the Internet are dead. No matter how many photos are taken, the last one will always overwrite the previous one. Careful students can also set this string, such as adding the word "IMG". Then, they will find that the folder "myimage" in the SD card will save the photos just taken by calling the camera, the photo name will not be repeated.
String str = null;
Date date = null;
SimpleDateFormat format = new SimpleDateFormat ("yyyyMMddHHmmss"); // obtain the current time and further convert it to a string
Date = new Date (resultCode );
Str = format. format (date );
String fileName = "/sdcard/myImage/" + str + ". jpg ";
SendBroadcast (fileName );
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 ();
}
} Break;
Case Activity. RESULT_CANCELED: // cancel
Break;
}
Break;
Case 2:
Switch (resultCode ){
Case Activity. RESULT_ OK :{
Uri uri = data. getData ();
Cursor cursor = mActivity. getContentResolver (). query (uri, null,
Null, null, null );
Cursor. moveToFirst ();
String imgNo = cursor. getString (0); // Image number
String imgPath = cursor. getString (1); // path of the image file
String imgSize = cursor. getString (2); // image size
String imgName = cursor. getString (3); // Image File Name
Cursor. close ();
// Options options = new BitmapFactory. Options ();
// Options. inJustDecodeBounds = false;
// Options. inSampleSize = 10;
// Bitmap bitmap = BitmapFactory. decodeFile (imgPath, options );
}
Break;
Case Activity. RESULT_CANCELED: // cancel
Break;
}
Break;
}

Remember to add permissions
<Uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
<Uses-permission android: name = "android. permission. CAMERA"/>
<Uses-permission android: name = "android. permission. RECORD_AUDIO"/>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>

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.