我正在开发一款相机应用。图片正在成功拍摄并保存。问题是保存的图像在查看时出现旋转。我在互联网上看到几个例子来旋转保存的图像。我的代码如下:
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data
.length);
try {
ContentValues v = new ContentValues();
v.put(MediaStore.Images.Media.TITLE,
"test"+System.currentTimeMillis());
v.put(MediaStore.Images.Media.DISPLAY_NAME,
"test"+System.currentTimeMillis());
v.put(MediaStore.Images.Media.DESCRIPTION, "Test picture");
v.put(MediaStore.Images.Media.DATE_ADDED,
System.currentTimeMillis());
v.put(MediaStore.Images.Media.DATE_TAKEN,
System.currentTimeMillis());
v.put(MediaStore.Images.Media.DATE_MODIFIED,
System.currentTimeMillis());
v.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
v.put(MediaStore.Images.Media.ORIENTATION,90);
Uri uriTarget =
getContentResolver().
insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, v);
String imagePath = getRealPathFromURI(uriTarget);
ExifInterface ei = new ExifInterface(imagePath);
int orientation =
ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
data= RotateBitmap(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
data= RotateBitmap(bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
data= RotateBitmap(bitmap, 180);
break;
// etc.
}
OutputStream imageFileOS;
imageFileOS =
getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close();
Log.d("URI",uriTarget.toString());
} catch (Exception e) {
e.printStackTrace();
}
finally
{
camera.startPreview();
}
}
};
以下是获取已保存图像路径的功能:
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(this, contentUri, proj, null,
null, null);
Cursor cursor = loader.loadInBackground();
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
问题是ExifInterface ei = new ExifInterface(imagePath); E / JHEAD说:不能打开' /storage/emulated/10/Pictures/1442234633142.jpg'