我需要在Android上从相机加载图片。我用这段代码打开相机:
private void openCamera()
{
mMediaUri =getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
startActivityForResult(photoIntent, REQUEST_TAKE_PHOTO);
}
private Uri getOutputMediaFileUri(int mediaTypeImage)
{
//check for external storage
if(isExternalStorageAvaiable())
{
File mediaStorageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
String fileName = "";
String fileType = "";
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new java.util.Date());
fileName = "IMG_"+timeStamp;
fileType = ".jpg";
File mediaFile;
try
{
mediaFile = File.createTempFile(fileName,fileType,mediaStorageDir);
absolutePath = mediaFile.getAbsolutePath();
}
catch (IOException e)
{
e.printStackTrace();
Log.i("St","Error creating file: " + mediaStorageDir.getAbsolutePath() +fileName +fileType);
return null;
}
return FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", mediaFile);
}
//something went wrong
return null;
}
private boolean isExternalStorageAvaiable()
{
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state))
{
return true;
}
else
{
return false;
}
}
这是onActivityResult()
中的签到:
else if(resultCode == RESULT_OK)
{
if (requestCode == REQUEST_TAKE_PHOTO || requestCode == REQUEST_PICK_PHOTO) //dalla fotocamera
{
if (data != null) //caso galleria
{
mMediaUri = data.getData();
Glide.with(getActivity()).load(mMediaUri).centerCrop().into(photo);
}
else
{
Glide.with(getActivity())
.load(mMediaUri)
.bitmapTransform(new CenterCrop(getActivity()), new RoundedCornersTransformation(getActivity(), 15, 2))
.into(photo);
}
}
问题在于某些设备(如安装了Nougat的Galaxy S4)Glide将图像加载90度而不是正确加载图像。我已经知道Glide默认会管理ExifInterface
,但在这种情况下它并不起作用。
我用相机加载时只有这个问题:使用图库或字节数组,它以正确的模式加载所有内容。
有办法解决吗? 感谢
答案 0 :(得分:0)
最后我发现了问题,这两行:
fileName = "IMG_"+timeStamp;
fileType = ".jpg";
使用Bitmap而不是Jpeg来解决问题:
fileName = "IMG_"+timeStamp;
fileType = ".bmp";