如果我像这样启动相机,如何防止Android相机将方向切换到横向:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imageFolder = new File (Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DCIM);
imageFile = new File (imageFolder, UUID.randomUUID().toString()+".png");
Uri uriImage = Uri.fromFile(imageFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriImage);
startActivityForResult(intent, 0);
所以我没有特定的相机活动。在manifest.xml中,整个应用程序设置为纵向,但摄像机会切换。
第二个问题,在拍摄照片并将其设置为imageView之后,即使我在纵向模式下拍摄它(我在设置imageView之前保存图像),它也会切换到方向,如何在正确的位置显示它?
答案 0 :(得分:0)
尝试将锁相机方向设为肖像,
// *************************************************************************//
// Stop the screen orientation changing during an event
// *************************************************************************//
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
lockScreenRotation(Configuration.ORIENTATION_PORTRAIT);
}
private void lockScreenRotation(int orientation)
{
// Stop the screen orientation changing during an event
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
// *************************************************************************//
// store the file url as it will be null after returning from camera app
// *************************************************************************//
@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
// save file url in bundle as it will be null on scren orientation
// changes
outState.putParcelable("file_uri", fileUri);
}
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onSaveInstanceState(savedInstanceState);
// get the file url
fileUri = savedInstanceState.getParcelable("file_uri");
}
在onActivityResult上设置此项以使图片处于正确位置
if (resultCode == Activity.RESULT_OK
&& requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE)
{
filePath = fileUri.getPath();
Utility.log("FILE PATH CAMEREA:->", filePath);
// AppSharedPrefrence.getInstance(getActivity()).setImagePath(path);
// *************************************************************************//
// set default camera rotation
// *************************************************************************//
try
{
File f = new File(filePath);
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int angle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
{
angle = 90;
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
{
angle = 180;
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
{
angle = 270;
}
Matrix mat = new Matrix();
mat.postRotate(angle);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bmp1 = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
Bitmap bmp = Bitmap.createBitmap(bmp1, 0, 0, bmp1.getWidth(), bmp1.getHeight(), mat, true);
OutputStream stream = new FileOutputStream(filePath);
bmp.compress(Bitmap.CompressFormat.JPEG, 70, stream);
// imageBmp =
// Bitmap.createScaledBitmap(BitmapFactory.decodeFile(path),
// 400, 400, true);
imageBmp = BitmapFactory.decodeFile(filePath);
imgProfilePic.setImageBitmap(imageBmp);
}
catch (IOException e)
{
e.printStackTrace();
}
catch (OutOfMemoryError oom)
{
oom.printStackTrace();
}
}