当我从图库中选择照片或从相机拍摄照片时,我的照片正在旋转?
问题描述:
即时通讯从图片库中选择一张照片或从相机拍摄照片,当它设置为一个ImageView它获得旋转,我如何解决它成为没有旋转?当我从图库中选择照片或从相机拍摄照片时,我的照片正在旋转?
public void setNewImage() {
new android.app.AlertDialog.Builder(getActivity())
.setPositiveButton("camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
getActivity().startActivityForResult(takePicture, 0);
}
})
.setNegativeButton("gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
getActivity().startActivityForResult(pickPhoto, 1);
}
})
.show();
}
这里IM的图像设置为ImageView的:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
NewpostFragment.post_image.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
/* Uri picUri = data.getData();
filePath = getPath(picUri);
img.setImageURI(picUri);*/
}
break;
case 1:
if (resultCode == RESULT_OK && data != null && data.getData() != null) {
/*Uri picUri = data.getData();
filePath = getPath(picUri);
img.setImageURI(picUri);*/
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
NewpostFragment.post_image.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
}
答
首先,ACTION_IMAGE_CAPTURE
将不通过getData()
给你一个Uri
在onActivityResult()
。虽然一些越野车相机应用程序可能会这样做,但大多数不会。你的选择是:
提供
EXTRA_OUTPUT
在ACTION_IMAGE_CAPTURE
Intent
,在这种情况下,照片应存放在由Uri
你投入EXTRA_OUTPUT
标识的位置,或不提供
EXTRA_OUTPUT
,并使用getParcelableExtra("data")
从相机应用
得到的缩略图见this sample app使用ACTION_IMAGE_CAPTURE
与EXTRA_OUTPUT
。
在定位上,如果你走了EXTRA_OUTPUT
路径,你可以use android.support.media.ExifInterface
找出照片的方向,然后做一些事情来旋转图像匹配(例如,旋转ImageView
)。
请参阅this sample app使用ExifInterface
(尽管我使用的是与android.support.media.ExifInterface
不同的实现方式)。