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
- <span style="FONT-SIZE:18PX;" > Intent Intent = new Intent (Sendmicoblog. This, rotaingactivity. class);
- MyApp MyApp = (MyApp) getapplication ();
- Myapp.setname (bitmap); //The name after modification
- Myapp.setphoto (Isphoto);
- Startactivityforresult (Intent, 1);</span>
In the rotaingactivity, it can be rotated accordingly.
[Java]View Plaincopy
- <span style="FONT-SIZE:18PX;" > //Get bitmap
- MyApp MyApp = (MyApp) getapplication ();
- Bitmap = Myapp.getname ();
- System.out.println ("rotaingactivity w =" + bitmap.getwidth () + "H =" + bitmap.getheight ());
- Mybitmap = bitmap;
- Mywidth = Bitmap.getwidth ();
- Myheight = Bitmap.getheight ();
- //Create a Matrix object for manipulating pictures
- Matrix = new Matrix ();
- drawable drawable = imagedispose.bitmaptodrawable (bitmap);
- Imageview.setbackgrounddrawable (drawable);
- Btncancel.setonclicklistener (new Onclicklistener () {
- @Override
- public void OnClick (View v) {
- Rotaingactivity. This.finish ();
- }
- });
- Btnrightrotaing.setonclicklistener (new Onclicklistener () {
- @Override
- public void OnClick (View v) {
- Mybitmap = Rotaingimageview (-90);
- }
- });
- Btnleftrotaing.setonclicklistener (new Onclicklistener () {
- @Override
- public void OnClick (View v) {
- Mybitmap = Rotaingimageview (+90);
- }
- });
- Btnok.setonclicklistener (new Onclicklistener () {
- @Override
- public void OnClick (View v) {
- MyApp MyApp = (MyApp) getapplication ();
- Myapp.setname (MYBITMAP); //After modification
- System.out.println ("OK Resultcoder");
- Intent Intent = new Intent (rotaingactivity. This, Sendmicoblog. class);
- Setresult (6, intent);
- Rotaingactivity. This.finish ();
- }
- });
- }
- private Bitmap Rotaingimageview (int angle2) {
- //Rotate picture action
- Matrix.postrotate (Angle2);
- System.out.println ("angle2=" + angle2);
- //Create a new picture
- Bitmap Resizedbitmap = Bitmap.createbitmap (Bitmap, 0, 0,
- Mywidth, Myheight, Matrix, true);
- drawable drawable = imagedispose.bitmaptodrawable (Resizedbitmap);
- Imageview.setbackgrounddrawable (drawable);
- return resizedbitmap;
- }</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
- Bitmapfactory.options bitmapoptions = new Bitmapfactory.options ();
- Bitmapoptions.insamplesize = 8;
- File File = new file (Sd_card_temp_dir);
- /**
- * To get the rotation angle of the picture, some systems rotate the picture, some do not rotate
- */
- int degree = Imagedispose.readpicturedegree (File.getabsolutepath ());
[Java]View Plaincopy
- <span style="FONT-SIZE:18PX;" >bitmap Camerabitmap = Bitmapfactory.decodefile (Sd_card_temp_dir, bitmapoptions);
- bitmap = Camerabitmap;
- /**
- * Rotate the picture to the right direction
- */
- Bitmap = Imagedispose.rotaingimageview (degree, bitmap);
- Upload (bitmap);</span>
[Java]View Plaincopy
- <span style="FONT-SIZE:18PX;" > /**
- * Rotate Picture
- * @param angle
- * @param bitmap
- * @return Bitmap
- */
- public static Bitmap rotaingimageview (int angle, Bitmap Bitmap) {
- //Rotate picture action
- Matrix matrix = new Matrix ();
- Matrix.postrotate (angle);
- System.out.println ("angle2=" + angle);
- //Create a new picture
- Bitmap Resizedbitmap = Bitmap.createbitmap (Bitmap, 0, 0,
- Bitmap.getwidth (), Bitmap.getheight (), Matrix, true);
- return resizedbitmap;
- }</span>
[Java]View Plaincopy
- /**
- * Read Picture properties: Angle of rotation
- * @param path Image Absolute
- * @return degree angle of rotation
- */
- public static int readpicturedegree (String path) {
- int degree = 0;
- try {
- Exifinterface exifinterface = new Exifinterface (path);
- int orientation = Exifinterface.getattributeint (Exifinterface.tag_orientation, Exifinterface.orientation_ NORMAL);
- switch (orientation) {
- Case exifinterface.orientation_rotate_90:
- degree = 90;
- Break ;
- Case exifinterface.orientation_rotate_180:
- degree = 180;
- Break ;
- Case exifinterface.orientation_rotate_270:
- degree = 270;
- Break ;
- }
- } catch (IOException e) {
- E.printstacktrace ();
- }
- return degree;
- }
Fixed an issue where Android has photos uploaded after photos are rotated