Android calls the system's camera and library

Source: Internet
Author: User

Android phone has its own camera and library, we do the project sometimes used to upload images to the server, today did a project to use this feature, so put my code down and share with you, the first blog hope that the great God many criticisms.
First call the Android album and the camera Code:

The 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);

The code is as follows:
Intent i = new Intent (Intent.action_pick,
Android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//Call the Android library
Startactivityforresult (i, 2);

The code is as follows:
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Call this method back when you are finished taking a photo
Super.onactivityresult (Requestcode, ResultCode, data);
Case 1:
Switch (ResultCode) {
Case activity.result_ok://Photo Complete Click OK
String sdstatus = Environment.getexternalstoragestate ();
if (!sdstatus.equals (environment.media_mounted)) {//Detect if 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");//Get the data returned by the camera and convert it to Bitmap picture format
FileOutputStream B = null;
File File = new file ("/sdcard/pk4fun/");
File.mkdirs ();//Create a folder with the name Pk4fun//photo named, under the target folder, with the current time number string as the name, you can ensure that each photo name is different. Other demos on the Internet the names of the photos here are all dead, and it will happen no matter how many pictures are taken, the latter one always overwrites the previous photo. The careful classmate can also set this string, such as "IMG" and so on, then will find the SD card myimage this folder, will save just call camera photos taken out, the photo name will not be repeated.
String str = NULL;
Date date = null;
SimpleDateFormat format = new SimpleDateFormat ("YYYYMMDDHHMMSS");//Gets the current time, further converted 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, +, b);//write data to file
} catch (FileNotFoundException e) {
E.printstacktrace ();
} finally {
try {
B.flush ();
B.close ();
} catch (IOException e) {
E.printstacktrace ();
}
} break;
Case activity.result_canceled://Canceled
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); Picture number
String Imgpath = cursor.getstring (1); Picture file path
String imgsize = cursor.getstring (2); Picture size
String imgname = cursor.getstring (3); Picture 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://Canceled
Break
}
Break
}


Last remember add permission
<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"/>

Android calls the system's camera and library

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.