Call the Android system camera to take pictures and save

Source: Internet
Author: User
Tags dateformat

[Java] View Plaincopyprint?
  1. <span style="FONT-SIZE:18PX;"  > PackageCom.demo;
  2. Import Java.io.BufferedOutputStream;
  3. Import Java.io.File;
  4. Import java.io.FileNotFoundException;
  5. Import Java.io.FileOutputStream;
  6. Import java.io.IOException;
  7. Import Java.util.Calendar;
  8. Import Java.util.Locale;
  9. Import android.app.Activity;
  10. Import android.content.Intent;
  11. Import Android.graphics.Bitmap;
  12. Import Android.os.Bundle;
  13. Import android.os.Environment;
  14. Import Android.provider.MediaStore;
  15. Import Android.text.format.DateFormat;
  16. Import Android.util.Log;
  17. Import Android.view.View;
  18. Import Android.view.View.OnClickListener;
  19. Import Android.widget.Button;
  20. Import Android.widget.ImageView;
  21. Import Android.widget.Toast;
  22. Public class Mycameraactivity extends Activity {
  23. /** Called when the activity is first created. * /
  24. Private button button;
  25. @Override
  26. public void OnCreate (Bundle savedinstancestate) {
  27. super.oncreate (savedinstancestate);
  28. Setcontentview (R.layout.main);
  29. Button = (button) Findviewbyid (R.id.button);
  30. Button.setonclicklistener (new Onclicklistener () {
  31. @Override
  32. public void OnClick (View v) {
  33. //TODO auto-generated method stub
  34. Intent Intent = new Intent (mediastore.action_image_capture);
  35. Startactivityforresult (Intent, 1);
  36. }
  37. });
  38. }
  39. @Override
  40. protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
  41. //TODO auto-generated method stub
  42. Super.onactivityresult (Requestcode, ResultCode, data);
  43. if (ResultCode = = ACTIVITY.RESULT_OK) {
  44. String sdstatus = Environment.getexternalstoragestate ();
  45. if (!sdstatus.equals (environment.media_mounted)) { //Detect if SD is available
  46. LOG.I ("Testfile",
  47. "SD card isn't avaiable/writeable right now.");
  48. return;
  49. }
  50. String name = new DateFormat (). Format ("Yyyymmdd_hhmmss", Calendar.getinstance (Locale.china)) + ". jpg";
  51. Toast.maketext (this, name, Toast.length_long). Show ();
  52. Bundle bundle = Data.getextras ();
  53. Bitmap Bitmap = (Bitmap) bundle.get ("data"); Get the data returned by the camera and convert it to bitmap picture format
  54. FileOutputStream B = null;
  55. //???????????????????????????????  Why not directly saved in the System album location???????????????????
  56. File File = new file ("/sdcard/myimage/");
  57. File.mkdirs (); //Create a folder
  58. String fileName = "/sdcard/myimage/" +name;
  59. try {
  60. b = new FileOutputStream (FileName);
  61. Bitmap.compress (Bitmap.CompressFormat.JPEG, N, b); Writing data to a file
  62. } catch (FileNotFoundException e) {
  63. E.printstacktrace ();
  64. } finally {
  65. try {
  66. B.flush ();
  67. B.close ();
  68. } catch (IOException e) {
  69. E.printstacktrace ();
  70. }
  71. }
  72. ((ImageView) Findviewbyid (R.id.imageview)). Setimagebitmap (bitmap); //Display the picture in ImageView
  73. }
  74. }
  75. }
  76. </span>
    • Previous Android call system picture browser and return picture path
    • Next import Project: Some items are hidden because they are in the workspace directory
Top

Call the Android system camera to take pictures and save

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.