1. Call the System camera
1. Declaring constants and variables
2. button Click event to open the system camera
3. Overwrite Onactivityresult event receive photo return
4. Generate File name return path
5. Save the picture
[Java]View Plaincopy
- Private static final int take_picture = 1;
- Class Btngotoactivity02listener implements Onclicklistener {
- public void OnClick (View v) {
- Intent Intent = new Intent (mediastore.action_image_capture); //Call camera to take pictures
- Startactivityforresult (Intent, take_picture);
- }
- }
- @Override
- protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
- String filename = "";
- try {
- if (ResultCode = = Activity.result_ok && Requestcode = = take_picture) {
- Bundle bundle = Data.getextras ();
- Bitmap bmp = (Bitmap) bundle.get ("data");
- filename = saveimage (BMP);
- }
- } catch (Exception err) {
- LOG.E ("Exception", Err.getlocalizedmessage ());
- }
- }
- Public String Buildfilename () {
- Date now = new Date ();
- SimpleDateFormat formater = new SimpleDateFormat ("Yymmdd_hhmmss");
- String sdcard = environment.getexternalstoragedirectory () + "/";
- String BasePath = sdcard + "dier/photo/";
- File dir = new file (BasePath);
- Dir.mkdirs ();
- return BasePath + Formater.format (now) + ". jpg";
- }
- Public String saveimage (Bitmap bmp) throws Exception {
- File File = new file (Buildfilename ());
- File.createnewfile ();
- FileOutputStream OStream = new FileOutputStream (file);
- Bmp.compress (Bitmap.CompressFormat.JPEG, oStream); //100 is the picture quality, 0-100, the bigger the better
- Ostream.flush ();
- Ostream.close ();
- return File.getpath ();
- }
2. Save photos with full resolution
Using the above method, the saved image is only 1/4 of the original size, if you need to save large photos you need to use the following way
Cons: In addition to saving pictures in the specified directory, the next image will be saved under the system photo path.
[Java]View Plaincopy
- Private static final int take_picture = 1;
- Private String Temp_file = "";
- Class Btngotoactivity02listener implements Onclicklistener {
- public void OnClick (View v) {
- Temp_file = Buildfilename ();
- Intent Intent = new Intent (mediastore.action_image_capture); //Call camera to take pictures
- Intent.putextra (Mediastore.extra_output, Uri.fromfile (new File (Temp_file))); //Save picture address
- Startactivityforresult (Intent, take_picture);
- }
- }
- Public String Buildfilename () {
- Date now = new Date ();
- SimpleDateFormat formater = new SimpleDateFormat ("Yymmdd_hhmmss");
- String sdcard = environment.getexternalstoragedirectory () + "/";
- String BasePath = sdcard + "dier/photo/";
- File dir = new file (BasePath);
- Dir.mkdirs ();
- return BasePath + Formater.format (now) + ". jpg";
- }
- @Override
- protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
- String filename = "";
- try {
- if (ResultCode = = Activity.result_ok && Requestcode = = take_picture) {
- File File = new file (Temp_file);
- if (file.exists ()) {
- filename = File.getpath ();
- Toast.maketext (mainactivity. This, "picture saved in:" + filename, + ). Show ();
- }
- else {
- Toast.maketext (mainactivity. This, "Pro, I really can not help ah, you are using what program photos?" "," (). Show ();
- }
- }
- } catch (Exception err) {
- LOG.E ("Exception", Err.getlocalizedmessage ());
- }
- }
http://blog.csdn.net/dier4836/article/details/7364721