我是 android 新手。我有一个应用程序,它具有从图库上传图像或从相机拍摄图像的功能。下面我编写了获取图像的代码。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode!=0){
if(PICK_MEDIA_FROM_GALLERY == requestCode){
if(null != data){
Uri uri = data.getData();
//String type = getMimeType(uri);
filePath = getRealPathFromURI(uri);
checkFileSupport(filePath);
}
}else{
if(ACTION_TAKE_PHOTO == requestCode){
checkFileSupport(filePath);
}else if(ACTION_TAKE_VIDEO == requestCode){
handleCameraVideo();
}
}
}
}
此函数用于固定图像的写入方向。
public Bitmap fixOrientation(Bitmap bmp) {
try {
ExifInterface exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
if (orientation==6)
{
Matrix matrix = new Matrix();
matrix.postRotate(90);
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}
else if (orientation==8)
{
Matrix matrix = new Matrix();
matrix.postRotate(270);
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}
else if (orientation==3)
{
Matrix matrix = new Matrix();
matrix.postRotate(180);
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bmp;
}
我使用该功能将 Image 设置为 imageview
private void setImagePreview(final String realPath) throws IOException {
Display display = getWindowManager().getDefaultDisplay();
Bitmap bitmap = ImageUtils.getThumbnailBitmap(realPath, display.getWidth());
if(null!=bitmap){
//rotate image and save in the same filepath
bitmap = fixOrientation(bitmap);
FileOutputStream fOut = new FileOutputStream(filePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
RelativeLayout tempLayout = (RelativeLayout) findViewById(R.id.previewLayout);
tempLayout.setVisibility(View.VISIBLE);
ImageView previewImageView = (ImageView) findViewById(R.id.previewImageView);
previewImageView.setImageBitmap(bitmap);
}
}
我在相机中有一张图像。
当我第一次从图库中选择该图像时,
当我尝试更改附件并选择同一张图片时,视图如下所示
我该如何解决这个问题?谁能帮帮我?