Android 90-degree problem rotated when selecting a picture or taking a photo

Source: Internet
Author: User

Because the previous blog post ignored the point content, so fill in here, the following is to solve the photo or select the picture display when the picture rotated 90 degrees or other degrees of problems, so that the photo can be displayed on the front: the following:

First, look directly at the above post to shoot or finish the picture after processing part:

Java code
  1. @Override
  2. protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
  3. switch (resultcode) {
  4. case 1:
  5. if (data! = null) {
  6. ///Get the returned URI, basically select the photo when the return is in the form of a URI, but in the photo of the machine is the URI is empty, so pay special attention to
  7. Uri Mimagecaptureuri = Data.getdata ();
  8. //The URI returned is not empty, then the picture information data is obtained in the URI. If it's empty, then we're going to get it in the following way
  9. if (Mimagecaptureuri! = null) {
  10. SetImage (Mimagecaptureuri); //processing and displaying pictures based on URI
  11. }
  12. }
  13. Break ;
  14. Default:
  15. Break ;
  16. }
  17. }

Second: deal with the 90 degree problem and show:

Java code
  1. Private void SetImage (Uri mimagecaptureuri) {
  2. //Whether it is taking pictures or selecting pictures each picture has stored in the data also stored with corresponding rotation angle orientation value
  3. //So we take out the image is to remove the angle value so that the picture can be displayed correctly, without rotating the effect of viewing
  4. Contentresolver cr = this.getcontentresolver ();
  5. cursor cursor = cr.query (Mimagecaptureuri, NULL, NULL, NULL, or null ); From the database based on the URI
  6. if (cursor! = null) {
  7. Cursor.movetofirst (); //Move the cursor to the first place because the URI here is the one that contains the ID, so it's the only one that doesn't need to loop.
  8. String FilePath = cursor.getstring (Cursor.getcolumnindex ("_data")); Get Picture path
  9. String orientation = cursor.getstring (cursor
  10. . Getcolumnindex ("orientation")); //Get angle of rotation
  11. Cursor.close ();
  12. if (filePath! = null) {
  13. Bitmap Bitmap = Bitmapfactory.decodefile (FilePath); //Read resource picture according to Path
  14. int angle = 0;
  15. if (orientation ! = NULL &&! "". Equals (orientation)) {
  16. Angle = Integer.parseint (orientation);
  17. }
  18. if (angle! = 0) {
  19. //The main function of the following method is to turn the picture to an angle, can also zoom out and so on
  20. Matrix M = new Matrix ();
  21. int width = bitmap.getwidth ();
  22. int height = bitmap.getheight ();
  23. M.setrotate (angle); //Rotation angle degree
  24. Bitmap = Bitmap.createbitmap (bitmap, 0, 0, width, height,
  25. M, true); //New Image generation
  26. }
  27. Photo.setimagebitmap (bitmap);
  28. }
  29. }
  30. }

OK to complete, need to take pictures and select the part of the picture function see http://104zz.iteye.com/blog/1687662

Android 90-degree problem rotated when selecting a picture or taking a photo

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.