Android calls photo album to achieve system control scaling and image Cutting

Source: Internet
Author: User

Http://blog.csdn.net/xys289187120/article/details/6575830

In Android, if you want to process images, you can call the system controls to achieve very good effects of scaling and cutting images. Today, I wrote a demo to share with you.

[Java]
View plaincopyprint?
  1. Package CN. m15.test;
  2. Import java. Io. bytearrayoutputstream;
  3. Import java. Io. file;
  4. Import Android. App. activity;
  5. Import Android. content. intent;
  6. Import Android. Graphics. Bitmap;
  7. Import android.net. Uri;
  8. Import Android. OS. Bundle;
  9. Import Android. OS. environment;
  10. Import Android. provider. mediastore;
  11. Import Android. View. view;
  12. Import Android. View. View. onclicklistener;
  13. Import Android. widget. Button;
  14. Import Android. widget. imageview;
  15. Public class testactivity
    Extends activity {
  16. Public static
    Final int none =
    0;
  17. Public static
    Final int photohraph = 1; // photograph
  18. Public static
    Final int photozoom =
    2; // zoom
  19. Public static
    Final int photoresoult =
    3; // result
  20. Public static
    Final string image_unspecified = "image /*";
  21. Imageview = NULL;
  22. Button button0 = NULL;
  23. Button button1 = NULL;
  24. @ Override
  25. Public void oncreate (bundle savedinstancestate ){
  26. Super. oncreate (savedinstancestate );
  27. Setcontentview (R. layout. Main );
  28. Imageview = (imageview) findviewbyid (R. Id. imageid );
  29. Button0 = (button) findviewbyid (R. Id. btn_01 );
  30. Button1 = (button) findviewbyid (R. Id. btn_02 );
  31. Button0.setonclicklistener (New onclicklistener (){
  32. @ Override
  33. Public void onclick (view v ){
  34. Intent intent = new intent (intent. action_pick,
    Null );
  35. Intent. setdataandtype (mediastore. Images. Media. external_content_uri, image_unspecified );
  36. Startactivityforresult (intent, photozoom );
  37. }
  38. });
  39. Button1.setonclicklistener (New onclicklistener (){
  40. @ Override
  41. Public void onclick (view v ){
  42. Intent intent = new intent (mediastore. action_image_capture );
  43. Intent. putextra (mediastore. extra_output, Uri. fromfile (new file (environment. getexternalstoragedirectory (),
    "Temp.jpg ")));
  44. Startactivityforresult (intent, photohraph );
  45. }
  46. });
  47. }
  48. @ Override
  49. Protected void onactivityresult (INT requestcode,
    Int resultcode, intent data ){
  50. If (resultcode = none)
  51. Return;
  52. // Take a photo
  53. If (requestcode = photohraph ){
  54. // Set the file storage path, which is stored in the following directory.
  55. File picture = new file (environment. getexternalstoragedirectory () +
    "/Temp.jpg ");
  56. Startphotozoom (URI. fromfile (picture ));
  57. }
  58. If (Data = NULL)
  59. Return;
  60. // Read the scaled image of the album
  61. If (requestcode = photozoom ){
  62. Startphotozoom (data. getdata ());
  63. }
  64. // Processing result
  65. If (requestcode = photoresoult ){
  66. Bundle extras = data. getextras ();
  67. If (extras! = NULL ){
  68. Bitmap photo = extras. getparcelable ("data ");
  69. Bytearrayoutputstream stream = new bytearrayoutputstream ();
  70. Photo. Compress (bitmap. compressformat. JPEG, 75, stream); // (0-100) compressed file
  71. Imageview. setimagebitmap (photo );
  72. }
  73. }
  74. Super. onactivityresult (requestcode, resultcode, data );
  75. }
  76. Public void startphotozoom (URI ){
  77. Intent intent = new intent ("com. Android. Camera. Action. Crop ");
  78. Intent. setdataandtype (Uri, image_unspecified );
  79. Intent. putextra ("crop ",
    "True ");
  80. // Aspectx aspecty is the aspect ratio
  81. Intent. putextra ("aspectx ",
    1 );
  82. Intent. putextra ("aspecty", 1 );
  83. // Outputx outputy indicates the width and height of the cropped image.
  84. Intent. putextra ("outputx", 64 );
  85. Intent. putextra ("outputy ",
    64 );
  86. Intent. putextra ("Return-Data ",
    True );
  87. Startactivityforresult (intent, photoresoult );
  88. }
  89. }

Package CN. m15.test; </P> <p> Import Java. io. bytearrayoutputstream; <br/> Import Java. io. file; <br/> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. graphics. bitmap; <br/> Import android.net. uri; <br/> Import android. OS. bundle; <br/> Import android. OS. environment; <br/> Import android. provider. mediastore; <br/> Import android. view. view; <br/> Import android. view. view. oncli Cklistener; <br/> Import android. widget. button; <br/> Import android. widget. imageview; </P> <p> public class testactivity extends activity {</P> <p> Public static final int none = 0; <br/> Public static final int photohraph = 1; // photograph <br/> Public static final int photozoom = 2; // scale <br/> Public static final int photoresoult = 3; // result </P> <p> Public static final string image_unspecified = "image /*"; <br/> IMA Geview imageview = NULL; <br/> button button0 = NULL; <br/> button button1 = NULL; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> imageview = (imageview) findviewbyid (R. id. imageid); <br/> button0 = (button) findviewbyid (R. id. btn_01); <br/> button1 = (button) findviewbyid (R. id. btn_02); </P> <p> button 0. setonclicklistener (New onclicklistener () {<br/> @ override <br/> Public void onclick (view v) {<br/> intent = new intent (intent. action_pick, null); <br/> intent. setdataandtype (mediastore. images. media. external_content_uri, image_unspecified); <br/> startactivityforresult (intent, photozoom); <br/>}< br/> }); </P> <p> button1.setonclicklistener (New onclicklistener () {</P> <p> @ override <br/> Public void Onclick (view v) {<br/> intent = new intent (mediastore. action_image_capture); <br/> intent. putextra (mediastore. extra_output, Uri. fromfile (new file (environment. getexternalstoragedirectory (), "temp.jpg"); <br/> startactivityforresult (intent, photohraph); <br/>}< br/> }); <br/>}</P> <p> @ override <br/> protected void onactivityresult (INT requestcode, int resultcode, intent data) {<br/> If (resultc Ode = none) <br/> return; <br/> // photograph <br/> If (requestcode = photohraph) {<br/> // set the file storage path, which is stored in the following directory <br/> file picture = new file (environment. getexternalstoragedirectory () + "/temp.jpg"); <br/> startphotozoom (URI. fromfile (picture); <br/>}</P> <p> If (Data = NULL) <br/> return; </P> <p> // read the scaled image of the album. <br/> If (requestcode = photozoom) {<br/> startphotozoom (data. getdata (); <br/>}< br/> // processing result <br/> If (reque Stcode = photoresoult) {<br/> bundle extras = data. getextras (); <br/> If (extras! = NULL) {<br/> bitmap photo = extras. getparcelable ("data"); <br/> bytearrayoutputstream stream = new bytearrayoutputstream (); <br/> photo. compress (bitmap. compressformat. JPEG, 75, stream); // (0-100) compressed file <br/> imageview. setimagebitmap (photo); <br/>}</P> <p> super. onactivityresult (requestcode, resultcode, data); <br/>}</P> <p> Public void startphotozoom (URI) {<br/> intent = new intent ("com. android. camera. action. crop "); <br/> intent. setdataandtype (Uri, image_unspecified); <br/> intent. putextra ("crop", "true"); <br/> // aspectx aspecty is the ratio of width to height <br/> intent. putextra ("aspectx", 1); <br/> intent. putextra ("aspecty", 1); <br/> // outputx outputy is the width and height of the cropped image. <br/> intent. putextra ("outputx", 64); <br/> intent. putextra ("outputy", 64); <br/> intent. putextra ("Return-Data", true); <br/> startactivityforresult (intent, photoresoult); <br/>}< br/>}[HTML]
View plaincopyprint?

  1. <? XML
    Version = "1.0"
    Encoding = "UTF-8"?>
  2. <Linearlayout
    Xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: Orientation = "vertical"
    Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent">
  5. <Textview
    Android: layout_width = "fill_parent"
  6. Android: layout_height = "wrap_content"
    Android: text = "@ string/hello"
    />
  7. <Imageview
    Android: Id = "@ + ID/imageid"
  8. Android: adjustviewbounds = "true"
    Android: maxwidth = "50dip"
  9. Android: maxheight = "50dip"
    Android: layout_width = "wrap_content"
  10. Android: layout_height = "wrap_content"
    />
  11. <Button
    Android: Id = "@ + ID/btn_01"
    Android: layout_height = "50dip"
  12. Android: text = "album"
    Android: layout_width = "150dip"/>
  13. <Button
    Android: Id = "@ + ID/btn_02"
    Android: layout_height = "50dip"
  14. Android: text = "photo"
    Android: layout_width = "150dip"/>
  15. </Linearlayout>

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.