Android Extensions-photo (camera)

Source: Internet
Author: User
Tags date now

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
  1. Private static final int take_picture = 1;
  2. Class Btngotoactivity02listener implements Onclicklistener {
  3. public void OnClick (View v) {
  4. Intent Intent = new Intent (mediastore.action_image_capture); //Call camera to take pictures
  5. Startactivityforresult (Intent, take_picture);
  6. }
  7. }
  8. @Override
  9. protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
  10. String filename = "";
  11. try {
  12. if (ResultCode = = Activity.result_ok && Requestcode = = take_picture) {
  13. Bundle bundle = Data.getextras ();
  14. Bitmap bmp = (Bitmap) bundle.get ("data");
  15. filename = saveimage (BMP);
  16. }
  17. } catch (Exception err) {
  18. LOG.E ("Exception", Err.getlocalizedmessage ());
  19. }
  20. }
  21. Public String Buildfilename () {
  22. Date now = new Date ();
  23. SimpleDateFormat formater = new SimpleDateFormat ("Yymmdd_hhmmss");
  24. String sdcard = environment.getexternalstoragedirectory () + "/";
  25. String BasePath = sdcard + "dier/photo/";
  26. File dir = new file (BasePath);
  27. Dir.mkdirs ();
  28. return BasePath + Formater.format (now) + ". jpg";
  29. }
  30. Public String saveimage (Bitmap bmp) throws Exception {
  31. File File = new file (Buildfilename ());
  32. File.createnewfile ();
  33. FileOutputStream OStream = new FileOutputStream (file);
  34. Bmp.compress (Bitmap.CompressFormat.JPEG, oStream); //100 is the picture quality, 0-100, the bigger the better
  35. Ostream.flush ();
  36. Ostream.close ();
  37. return File.getpath ();
  38. }



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
    1. Private static final int take_picture = 1;
    2. Private String Temp_file = "";
    3. Class Btngotoactivity02listener implements Onclicklistener {
    4. public void OnClick (View v) {
    5. Temp_file = Buildfilename ();
    6. Intent Intent = new Intent (mediastore.action_image_capture); //Call camera to take pictures
    7. Intent.putextra (Mediastore.extra_output, Uri.fromfile (new File (Temp_file))); //Save picture address
    8. Startactivityforresult (Intent, take_picture);
    9. }
    10. }
    11. Public String Buildfilename () {
    12. Date now = new Date ();
    13. SimpleDateFormat formater = new SimpleDateFormat ("Yymmdd_hhmmss");
    14. String sdcard = environment.getexternalstoragedirectory () + "/";
    15. String BasePath = sdcard + "dier/photo/";
    16. File dir = new file (BasePath);
    17. Dir.mkdirs ();
    18. return BasePath + Formater.format (now) + ". jpg";
    19. }
    20. @Override
    21. protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
    22. String filename = "";
    23. try {
    24. if (ResultCode = = Activity.result_ok && Requestcode = = take_picture) {
    25. File File = new file (Temp_file);
    26. if (file.exists ()) {
    27. filename = File.getpath ();
    28. Toast.maketext (mainactivity.   This, "picture saved in:" + filename, + ). Show ();
    29. }
    30. else {
    31. Toast.maketext (mainactivity. This, "Pro, I really can not help ah, you are using what program photos?"  "," (). Show ();
    32. }
    33. }
    34. } catch (Exception err) {
    35. LOG.E ("Exception", Err.getlocalizedmessage ());
    36. }
    37. }

http://blog.csdn.net/dier4836/article/details/7364721

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.