【发布时间】:2017-03-21 03:19:32
【问题描述】:
当我在我的 Android One 手机或模拟器本身上测试我的应用程序时,一切正常,但在其他一些设备上出现问题。
问题是,我基本上发送一个相机意图,在用户拍照后从意图中获取数据,然后我用从相机得到的任何东西设置ImageView 的像素。对于某些设备(主要是三星),图像会旋转,拍摄时不会显示。
我的应用只能在纵向模式下工作,但如果用户在拍照时旋转手机,也可以在横向模式下拍照。
有没有办法检测设备旋转图像的默认角度,所以我在拍摄图像后旋转位图?
代码如下:
发送意图:
File path = new File(getActivity().getFilesDir(), "map_roomie");
if (!path.exists()) path.mkdirs();
mFileName = createImageFileName();
File image = new File(path, mFileName);
Uri imageUri = FileProvider.getUriForFile(getActivity(), AddRoomFragment.CAPTURE_IMAGE_FILE_PROVIDER, image);
Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(imageCaptureIntent, AddRoomFragment.CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
捕捉片段的意图
File path = new File(getActivity().getFilesDir(), "map_roomie");
if (!path.exists()) path.mkdirs();
File imageFile = new File(path, mFileName);
setBitmapOfImageView(mCurrentPhotoId, decodeAndReturnBitmap(imageFile.getAbsolutePath()));
辅助函数:
public void setBitmapOfImageView(int photoId, Bitmap bitmap)
{
mPhotos[photoId].setImageBitmap(bitmap);
mPhotosState[photoId] = 1;
}
public Bitmap decodeAndReturnBitmap(String filePath)
{
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
final int REQUIRED_SIZE = 512;
int widthTemp = o.outWidth, heightTemp = o.outHeight;
int scale = 1;
while (true) {
if (widthTemp < REQUIRED_SIZE && heightTemp < REQUIRED_SIZE) {
break;
}
widthTemp /= 2;
heightTemp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(filePath, o2);
}
【问题讨论】:
标签: android