Fixed an issue where Android has photos uploaded after photos are rotated

Source: Internet
Author: User

Go to http://blog.csdn.net/walker02/article/details/8211628

Needs: To do Sina Weibo projects, to upload pictures and two outside a project to use the phone to take pictures, both of which need to upload pictures to the server

Encounter problems: Some mobile phone shot pictures rotated 90 degrees, some pictures rotated 180 degrees, some phones are normal, the server is a positive, so the problem comes, not users can send a photo on the micro-Bo saw is rotated Ah, another project rotated pictures directly match the problem, this is more serious.

Solution: At the beginning of the internet did not find a good solution, Google Baidu search a pass, think of the first solution, when the phone photo end, in the return of the results of processing immediately jump to a new page, in the new page let the user manually to rotate the picture, and then click OK. Can be uploaded to the server is the image of the second solution, which is found in the forum a method, you can get the properties of the picture, read the rotation angle of the picture.

The first way of code:

Handling jumps in Onactivityresult

[Java]View Plaincopy
    1. <span style="FONT-SIZE:18PX;" > Intent Intent = new Intent (Sendmicoblog. This, rotaingactivity.  class);
    2. MyApp MyApp = (MyApp) getapplication ();
    3. Myapp.setname (bitmap); //The name after modification
    4. Myapp.setphoto (Isphoto);
    5. Startactivityforresult (Intent, 1);</span>


In the rotaingactivity, it can be rotated accordingly.

[Java]View Plaincopy
  1. <span style="FONT-SIZE:18PX;" > //Get bitmap
  2. MyApp MyApp = (MyApp) getapplication ();
  3. Bitmap = Myapp.getname ();
  4. System.out.println ("rotaingactivity w =" + bitmap.getwidth () + "H =" + bitmap.getheight ());
  5. Mybitmap = bitmap;
  6. Mywidth = Bitmap.getwidth ();
  7. Myheight = Bitmap.getheight ();
  8. //Create a Matrix object for manipulating pictures
  9. Matrix = new Matrix ();
  10. drawable drawable = imagedispose.bitmaptodrawable (bitmap);
  11. Imageview.setbackgrounddrawable (drawable);
  12. Btncancel.setonclicklistener (new Onclicklistener () {
  13. @Override
  14. public void OnClick (View v) {
  15. Rotaingactivity.  This.finish ();
  16. }
  17. });
  18. Btnrightrotaing.setonclicklistener (new Onclicklistener () {
  19. @Override
  20. public void OnClick (View v) {
  21. Mybitmap = Rotaingimageview (-90);
  22. }
  23. });
  24. Btnleftrotaing.setonclicklistener (new Onclicklistener () {
  25. @Override
  26. public void OnClick (View v) {
  27. Mybitmap = Rotaingimageview (+90);
  28. }
  29. });
  30. Btnok.setonclicklistener (new Onclicklistener () {
  31. @Override
  32. public void OnClick (View v) {
  33. MyApp MyApp = (MyApp) getapplication ();
  34. Myapp.setname (MYBITMAP); //After modification
  35. System.out.println ("OK Resultcoder");
  36. Intent Intent = new Intent (rotaingactivity. This, Sendmicoblog.  class);
  37. Setresult (6, intent);
  38. Rotaingactivity.  This.finish ();
  39. }
  40. });
  41. }
  42. private Bitmap Rotaingimageview (int angle2) {
  43. //Rotate picture action
  44. Matrix.postrotate (Angle2);
  45. System.out.println ("angle2=" + angle2);
  46. //Create a new picture
  47. Bitmap Resizedbitmap = Bitmap.createbitmap (Bitmap, 0, 0,
  48. Mywidth, Myheight, Matrix, true);
  49. drawable drawable = imagedispose.bitmaptodrawable (Resizedbitmap);
  50. Imageview.setbackgrounddrawable (drawable);
  51. return resizedbitmap;
  52. }</span>


The second way: it is much simpler, directly out of their own to steal the photos to deal with, do not need the user's operation, so the experience will be better.

Handle it in Onactivityresult.

[Java]View Plaincopy
    1. Bitmapfactory.options bitmapoptions = new Bitmapfactory.options ();
    2. Bitmapoptions.insamplesize = 8;
    3. File File = new file (Sd_card_temp_dir);
    4. /**
    5. * To get the rotation angle of the picture, some systems rotate the picture, some do not rotate
    6. */
    7. int degree = Imagedispose.readpicturedegree (File.getabsolutepath ());



[Java]View Plaincopy
    1. <span style="FONT-SIZE:18PX;"  >bitmap Camerabitmap = Bitmapfactory.decodefile (Sd_card_temp_dir, bitmapoptions);
    2. bitmap = Camerabitmap;
    3. /**
    4. * Rotate the picture to the right direction
    5. */
    6. Bitmap = Imagedispose.rotaingimageview (degree, bitmap);
    7. Upload (bitmap);</span>

[Java]View Plaincopy
  1. <span style="FONT-SIZE:18PX;" > /**
  2. * Rotate Picture
  3. * @param angle
  4. * @param bitmap
  5. * @return Bitmap
  6. */
  7. public static Bitmap rotaingimageview (int angle, Bitmap Bitmap) {
  8. //Rotate picture action
  9. Matrix matrix = new Matrix ();
  10. Matrix.postrotate (angle);
  11. System.out.println ("angle2=" + angle);
  12. //Create a new picture
  13. Bitmap Resizedbitmap = Bitmap.createbitmap (Bitmap, 0, 0,
  14. Bitmap.getwidth (), Bitmap.getheight (), Matrix, true);
  15. return resizedbitmap;
  16. }</span>


[Java]View Plaincopy
    1. /**
    2. * Read Picture properties: Angle of rotation
    3. * @param path Image Absolute
    4. * @return degree angle of rotation
    5. */
    6. public static int readpicturedegree (String path) {
    7. int degree = 0;
    8. try {
    9. Exifinterface exifinterface = new Exifinterface (path);
    10. int orientation = Exifinterface.getattributeint (Exifinterface.tag_orientation, Exifinterface.orientation_  NORMAL);
    11. switch (orientation) {
    12. Case exifinterface.orientation_rotate_90:
    13. degree = 90;
    14. Break ;
    15. Case exifinterface.orientation_rotate_180:
    16. degree = 180;
    17. Break ;
    18. Case exifinterface.orientation_rotate_270:
    19. degree = 270;
    20. Break ;
    21. }
    22. } catch (IOException e) {
    23. E.printstacktrace ();
    24. }
    25. return degree;
    26. }

Fixed an issue where Android has photos uploaded after photos are rotated

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.