Demand: To do Sina Weibo project, can upload pictures and two foreign projects used to take pictures of mobile phones, both need to upload pictures to the server
Encountered problems: Some mobile phone pictures rotated 90 degrees, and some pictures rotated 180 degrees, some mobile phones are normal, the server is required to be positive, so the problem comes, not users can send a photo in micro Bo see is rotated Ah, another project in the rotation of the picture directly matching the problem, this more serious.
Solution: At the beginning of the web did not find a good solution, Google Baidu search a pass, think of the first solution, when the mobile phone camera end, in return to the results of processing immediately jump to a new page, in the new page let users manually to rotate the picture, and then click OK. You can upload the image to the server; the second solution, this is a method found in the forum, you can get the attributes of the picture, read the image rotation angle.
The first way code:
Processing jumps inside the Onactivityresult
<span style= "FONT-SIZE:18PX;" > Intent Intent = new Intent (sendmicoblog.this, rotaingactivity.class);
MYAPP MyApp = (MyApp) getapplication ();
Myapp.setname (bitmap);//After the modified name
Myapp.setphoto (isphoto);
Startactivityforresult (Intent, 1);</span>
In the rotaingactivity, it can be rotated accordingly.
<span style= "FONT-SIZE:18PX;"
>//Obtain 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 ();
Matrix of matrix = new Matrix () for manipulating pictures;
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) {my
APP 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 the stolen photos to deal with, do not require the user's operation, so experience will be better.
To deal with Onactivityresult inside.
Bitmapfactory.options bitmapoptions = new Bitmapfactory.options ();
Bitmapoptions.insamplesize = 8;
File File = new file (sd_card_temp_dir);
/**
* Get the rotation angle of the picture, some systems take pictures of the picture rotation, some do not rotate
/int degree = Imagedispose.readpicturedegree ( File.getabsolutepath ());
<span style= "FONT-SIZE:18PX;" >bitmap Camerabitmap = Bitmapfactory.decodefile (Sd_card_temp_dir, bitmapoptions);
bitmap = Camerabitmap;
/**
* To rotate the picture into a positive direction *
/bitmap = Imagedispose.rotaingimageview (degree, bitmap);
Upload (bitmap);</span>
<span style= "FONT-SIZE:18PX;" >/** * Rotate picture * @param angle * @param bitmap * @return Bitmap/public static bitmap Rotaingimag
Eview (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 (), MA
Trix, True);
return resizedbitmap; }</span>
/**
* Read Picture properties: Angle of rotation
* @param path Picture absolute path
* @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 =;
break;
Case exifinterface.orientation_rotate_180:
degree = 180;
break;
Case exifinterface.orientation_rotate_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printstacktrace ();
}
return degree;
}
This is done, and the photos you uploaded are positive.
Picture of the first way
The above is to solve the android some mobile phone photos uploaded after the rotation of the problem, thank you for your support for this site!