【发布时间】:2016-11-10 08:48:04
【问题描述】:
我正在从图库中选择一张图片并将其显示在图片视图中。图像被选中,但在三星手机上,它有旋转图像的问题,为了解决我正在检查图像是否旋转使用 EXIF 接口,如果旋转改变它的角度。
但这不适用于某些图像。有些图像我可以直接看到,但有些图像如果是直的,那么它们也会旋转。
当我调试图像的方向为 0 时,它会在默认情况下运行,并将普通位图应用于旋转位图。我看到的图像仍然是旋转的。不明白为什么会发生这种情况..
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bm = Bitmap.createScaledBitmap(bm,512,512, true);
bm.compress(Bitmap.CompressFormat.PNG,100, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".png");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
loadImageFromFile(destination.getAbsolutePath());
}
public void loadImageFromFile(String imageFile){
try {
ExifInterface ei = new ExifInterface(imageFile);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile);
Bitmap rotatedBitmap = null;
Log.e("orientation",String.valueOf(orientation)+" check");
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotatedBitmap = rotateImage(bitmap, 90);
Log.e("orientation",String.valueOf(orientation)+" check");
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotatedBitmap = rotateImage(bitmap, 180);
Log.e("orientation",String.valueOf(orientation)+" check");
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotatedBitmap = rotateImage(bitmap, 270);
Log.e("orientation",String.valueOf(orientation)+" check");
break;
case ExifInterface.ORIENTATION_NORMAL:
rotatedBitmap = bitmap;
Log.e("orientation",String.valueOf(orientation)+" check");
break;
default:
rotatedBitmap = bitmap;
Log.e("orientation",String.valueOf(orientation)+" check");
break;
}
if(rotatedBitmap != null)
{
profile_image.setImageBitmap(rotatedBitmap);
selectedBitmap = rotatedBitmap;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
selectedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); //replace 100 with desired quality percentage.
byte[] byteArray = stream.toByteArray();
File tempFile = File.createTempFile("temp",null, getCacheDir());
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(byteArray);
mProfileImage = tempFile;
}
}
catch (IOException ex) {
// UiUtils.showAlert(getString(R.string.error),NewGroupAcvitity.this);
}
}
public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix,
true);
}
编辑:
-
@SuppressWarnings("deprecation")
- private void onSelectFromGalleryResult(Intent data) {
-
- Uri uri = (Uri)data.getData();
- String[] filePathColumn = { MediaStore.Images.Media.DATA };
- Cursor cursor = getContentResolver().query(uri,filePathColumn, null, null, null);
- if(cursor != null) {
- cursor.moveToFirst();
- int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
- String picturePath = cursor.getString(columnIndex);
- cursor.close();
-
- loadImageFromFile(picturePath);
- }
- }
@greenapps - 像这样使用选定的文件?我试过了,但这在小米设备上不起作用,所以我更改了代码。其他设备还有其他方法吗?
这里出了什么问题?有人可以帮忙吗?谢谢。
【问题讨论】:
-
“rotatedBitmap = rotateImage(bitmap, 90);”中的“位图”在哪里从哪里来?
-
该代码不适用于小米。行。但它适用于三星吗?你不清楚。
-
在三星上工作,我可以直接看到图像。但是在小米中,光标返回 null 所以我无法选择图像。 @greenapps
-
如前所述:您应该使用不同的
ExifInterface类,它可以从文件的输入流中读取文件内容。Uri uri = (Uri)data.getData();。因此,您可以使用InputStream is = getContentResolver().openInputStream(uri);阅读 stackoverflow,正如 CommonsWare 在您可以使用哪个类之前所说的那样。 -
我没明白。你能举个例子吗? @greenapps
标签: android image bitmap rotation exif