Android phone has its own camera and graphics library, we do in the project is sometimes used to upload pictures to the server, today did a project to use this function, so put my code to write down and share with you, the first blog I hope that the great God a lot of criticism.
First, the code that calls the Android photo album and Camera:
Copy Code code as follows:
Intent Intent = new Intent (mediastore.action_image_capture);//Android camera with the call
Photouri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Startactivityforresult (Intent, 1);
Copy Code code as follows:
Intent i = new Intent (Intent.action_pick,
Android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//Calling the Android Gallery
Startactivityforresult (i, 2);
Copy Code code as follows:
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Call this method after completing the 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 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/pk4fun/");
File.mkdirs ();//Create a folder with the name Pk4fun//photo, and under the target folder, the name of the current time number string to ensure that each photo name is not the same. Other demo on the internet here's the name of the photos are written dead, it will happen no matter how many pictures, the latter one always put the previous photo coverage. Careful students can also set this string, such as "IMG" and so on, and then you will find the SD card myimage this folder, will save just call the camera to take out the photos, the photo name will not repeat.
String str = NULL;
Date date = null;
SimpleDateFormat format = new SimpleDateFormat ("YYYYMMDDHHMMSS");//Get current time, further convert to 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
}
Finally remember to 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"/>