在我的应用程序中,我通过相机捕获图像并将其路径保存在字符串变量(用于在服务器上发送图像的 SD 卡路径)中,那时我还在 ImageView 上设置了该图像。但是图像在横向中自动旋转,而不是按直角设置。我一直在 StackOverflow 和谷歌上搜索并找到 EXIF 轮换使用这个:
http://mobisocial.stanford.edu/news/2011/08/rotating-images-in-android/
但它不工作。我的代码是:(裁剪后的操作代码是onActivityResult)
case AppConstants.CROP_FROM_CAMERA:
if (data != null) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
File file = new File("/sdcard/bidnear/");
if (!file.isDirectory())
file.mkdir();
imageUrl = "/sdcard/bidnear/thumbimgcrop.png";
file = new File("/sdcard/bidnear/thumbimgcrop.png");
try {
photo = rotateImage(photo,mImageCaptureUri);
photo.compress(Bitmap.CompressFormat.PNG, 100,
new FileOutputStream(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
objimg.setBackgroundResource(0);
objimg.setImageBitmap(photo);
}
}
旋转方法是:
private Bitmap rotateImage(Bitmap objbitmap,Uri uri)
{
Matrix matrix = new Matrix();
float rotation =rotationForImage(MyProfile.this, uri);
if (rotation != 0f) {
matrix.preRotate(rotation);
}
Bitmap resizedBitmap = Bitmap.createBitmap(
objbitmap, 0, 0,80,80, matrix, true);
return resizedBitmap;
}
它不工作;我的打开相机和捕获代码是:
private void setUserImage() {
final String[] objimagechooseoptions = new String[] {
AppConstants.SELECT_CAMERA, AppConstants.SELECT_GALLERY };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.select_dialog_item, objimagechooseoptions);
AlertDialog.Builder objbuilder = new AlertDialog.Builder(this);
objbuilder.setTitle("Select Image");
objbuilder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) { // pick from
// camera
if (item == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "tmp_avatar_"
+ String.valueOf(System.currentTimeMillis())
+ ".png"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent,
AppConstants.PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
} else { // pick from file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"),
AppConstants.PICK_FROM_FILE);
}
}
});
final AlertDialog dialog = objbuilder.create();
dialog.show();
}