【发布时间】:2015-11-02 15:32:47
【问题描述】:
在我的代码中,我允许用户从图库或相机上传照片。然后通过改造将图像存储到服务器中。如果照片是使用手机的相机拍摄的(无论是通过应用程序调用还是通过相机直接调用),检索时照片始终旋转 90 度。如果图像不是使用相机拍摄的,则方向是正确的。我该如何解决这个问题?
我知道我是否尝试直接显示图像而不将其存储在服务器中,并且可能处于正确的方向,因为我可以在显示之前旋转它。我正在做类似于这里的代码的事情:https://gist.github.com/Mariovc/f06e70ebe8ca52fbbbe2。
但是因为我需要上传到服务器,然后再次从服务器检索。在存储到服务器之前如何旋转它,以便在检索它时它已经处于正确的方向?
以下是我的代码的一些部分(只是一些通常的图像处理,并且在服务完成图像下载后调用 displayAvatarInProfile()):
public void displayAvatarInProfile(String filePath) {
if(filePath != null && filePath != ""){
int targetW = mProfilePicImageView.getWidth();
int targetH = mProfilePicImageView.getHeight();
Bitmap bm = ImageStorageUtils.resizePic(filePath, targetW, targetH);
mProfilePicImageView.setImageBitmap(bm);
} else {
mProfilePicImageView.setImageBitmap(null);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Activity.RESULT_OK) {
Uri fileUri = null;
String filePath = null;
switch(requestCode) {
case REQUEST_PICK_IMAGE:
fileUri = data.getData();
break;
case REQUEST_CAPTURE_IMAGE:
File imageFile = ImageStorageUtils.getFile(
getActivity().getResources().getString(R.string.ApptTitle),
"avatar_" + mUser.getLogin());
filePath = imageFile.getAbsolutePath();
fileUri = ImageStorageUtils.getUriFromFilePath(mContext.get(), filePath);
break;
default:
break;
}
if(fileUri != null) {
getActivity().startService(ProfileService.makeIntentUploadAvatar(
mContext.get(), mUser.getId(), fileUri));
}
}
}
public void pickImage() {
final Intent imageGalleryIntent =
new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI)
.setType("image/*")
.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
// Verify the intent will resolve to an Activity.
if (imageGalleryIntent.resolveActivity(getActivity().getPackageManager()) != null)
// Start an Activity to get the Image from the Image Gallery
startActivityForResult(imageGalleryIntent,
DisplayProfileFragment.REQUEST_PICK_IMAGE);
}
public void captureImage() {
Uri captureImageUri = null;
try {
captureImageUri = Uri.fromFile(ImageStorageUtils.createImageFile(
mContext.get(),
getActivity().getResources().getString(R.string.ApptTitle),
"avatar_" + mUser.getLogin()));
} catch (IOException e) {
e.printStackTrace();
}
// Create an intent that will start an Activity to get
// capture an image.
final Intent captureImageIntent =
new Intent(MediaStore.ACTION_IMAGE_CAPTURE)
.putExtra(MediaStore.EXTRA_OUTPUT, captureImageUri);
// Verify the intent will resolve to an Activity.
if (captureImageIntent.resolveActivity(getActivity().getPackageManager()) != null)
// Start an Activity to capture an image
startActivityForResult(captureImageIntent,
DisplayProfileFragment.REQUEST_CAPTURE_IMAGE);
}
更新 1
有人评论说我需要更多描述:
当我上传文件时,我会这样做:
boolean succeeded = mImpatientServiceProxy.uploadAvatar(userId, new TypedFile("image/jpg", imageFile));
下载时,我得到一个 retrofit.client.Response 对象,然后我从响应中获取 InputStream 并使用输出流将数据写入文件。
final InputStream inputStream = response.getBody().in();
final OutputStream outputStream = new FileOutputStream(file);
IOUtils.copy(inputStream, outputStream);
更新 2
这不是现有解决方案的重复,因为用户可以从图库上传,您不知道它是从在线资源还是相机拍摄的照片,因此您在显示时无法旋转图像。
【问题讨论】:
-
我们看不到您上传的内容。一份文件?位图?我们也看不到您下载的内容和下载方式。
-
嗨@greenapps 请参考我上面的更新
-
why image captured using camera intent gets rotated on some devices in android 的可能副本。你应该打开
onActivityResult()收到的Jpeg文件,随意旋转,然后再次压缩成Jpeg。 -
抱歉,看到一个电话我看不到它是如何完成的。
-
@AlexCohn 哦...我想我没有仔细查看您发送的页面。因为那个时候我认为这不是同一个问题。是的,它对我有用。但是除了从 ExifInterface 中找出它的旋转之外,我还必须使用我自己的位图调整大小函数。谢谢!
标签: android image rotation android-camera