为啥人像视图照片水平显示?
Posted
技术标签:
【中文标题】为啥人像视图照片水平显示?【英文标题】:Why Do Portrait View Photos Display Horizontally?为什么人像视图照片水平显示? 【发布时间】:2016-02-28 21:44:42 【问题描述】:我希望有人可以帮助我解决这个问题,因为我是 java 的主要菜鸟,我尝试了很多代码,但似乎没有任何效果。当我在纵向视图中打开我的应用程序时会发生什么,它会以横向视图显示图像。
但是,如果我在横向视图中打开应用程序并拍照,它会以横向显示......它应该如何。
对于纵向视图,我该如何解决这个问题?
这是java的主要活动
package com.makingmemad.apk;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends Activity
private String mFileUri;
private final Context mContext = this;
@
Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnbutton1 = (Button) findViewById(R.id.button1);
btnbutton1.setOnClickListener(new OnClickListener()
@
Override
public void onClick(View v)
Uri uri = createPictureFile();
mFileUri = uri.getEncodedPath();
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, 100);
);
@
Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (requestCode == 100 && resultCode == RESULT_OK)
if (mFileUri != null)
Intent intent = new Intent(mContext, SecondActivity.class);
intent.putExtra("filepath", mFileUri);
startActivity(intent);
private static File getOutputMediaFile(int type)
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "DCIM/Camera");
if (!mediaStorageDir.exists())
if (!mediaStorageDir.mkdirs())
return null;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
File mediaFile;
if (type == 1) // image
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
else if (type == 2) // video
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
else
return null;
return mediaFile;
public Uri createPictureFile()
String storageState = Environment.getExternalStorageState();
if (storageState.equals(Environment.MEDIA_MOUNTED))
File pictureDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
pictureDir = new File(pictureDir, "MyApp");
// Create the storage directory if it does not exist
if (!pictureDir.exists())
if (!pictureDir.mkdirs())
Log.d("user", "failed to create directory");
return null;
//Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String fileName = pictureDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg";
File imageFile = new File(fileName);
// Convert to URI and return
return Uri.fromFile(imageFile);
else
Log.d("user", "No media mounted");
return null;
这是我的 acitivty_main.xml 文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/bg"
tools:context="com.MYAPPNAME.apk.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_
android:layout_
android:layout_above="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="86dp"
android:text="LET'S BEGIN"
android:textSize="20dp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="@+id/textView1"
android:layout_
android:layout_
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="72dp"
android:text="Before you begin, PLEASE NOTE that this game is intended for entertainment purposes only."
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textSize="24dp"
android:textStyle="bold" />
</RelativeLayout>
这是我的 SecondActivity.java 文件
package com.MYAPPNAME.apk;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import java.io.File;
public class SecondActivity extends Activity
@
Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
String filepath = intent.getStringExtra("filepath");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8; // down sizing image as it throws OutOfMemory Exception for larger images
filepath = filepath.replace("file://", ""); // remove to avoid BitmapFactory.decodeFile return null
File imgFile = new File(filepath);
if (imgFile.exists())
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);
imageView.setImageBitmap(bitmap);
这是我的 activity_second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/bg"
tools:context="com.MYAPPNAME.apk.SecondActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="185dp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
这是我的 ExifUtil 文件
package com.MYAPPNAME.apk;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Build;
public class ExifUtil
public Bitmap rotateBitmap(String path)
Bitmap myBitmap = BitmapFactory.decodeFile(path);
return ExifUtil.rotateBitmap(path, myBitmap);
/**
* @see http://sylvana.net/jpegcrop/exif_orientation.html
*/
public static Bitmap rotateBitmap(String src, Bitmap bitmap)
try
int orientation = getExifOrientation(src);
if (orientation == 1)
return bitmap;
Matrix matrix = new Matrix();
switch (orientation)
case 2:
matrix.setScale(-1, 1);
break;
case 3:
matrix.setRotate(180);
break;
case 4:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case 5:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case 6:
matrix.setRotate(90);
break;
case 7:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case 8:
matrix.setRotate(-90);
break;
default:
return bitmap;
try
Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return oriented;
catch (OutOfMemoryError e)
e.printStackTrace();
return bitmap;
catch (IOException e)
e.printStackTrace();
return bitmap;
private static int getExifOrientation(String src) throws IOException
int orientation = 1;
try
/**
* if your are targeting only api level >= 5
* ExifInterface exif = new ExifInterface(src);
* orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
*/
if (Build.VERSION.SDK_INT >= 5)
Class <? > exifClass = Class.forName("android.media.ExifInterface");
Constructor <? > exifConstructor = exifClass.getConstructor(new Class[]
String.class
);
Object exifInstance = exifConstructor.newInstance(new Object[]
src
);
Method getAttributeInt = exifClass.getMethod("getAttributeInt", new Class[]
String.class, int.class
);
Field tagOrientationField = exifClass.getField("TAG_ORIENTATION");
String tagOrientation = (String) tagOrientationField.get(null);
orientation = (Integer) getAttributeInt.invoke(exifInstance, new Object[]
tagOrientation, 1
);
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SecurityException e)
e.printStackTrace();
catch (NoSuchMethodException e)
e.printStackTrace();
catch (IllegalArgumentException e)
e.printStackTrace();
catch (InstantiationException e)
e.printStackTrace();
catch (IllegalAccessException e)
e.printStackTrace();
catch (InvocationTargetException e)
e.printStackTrace();
catch (NoSuchFieldException e)
e.printStackTrace();
return orientation;
【问题讨论】:
【参考方案1】:您在图像上有 exif 数据,可以告诉您方向。在将图像设置为图像视图之前,您必须根据 exif 数据旋转图像。 这是你可以使用的代码,像这样使用
public Bitmap rotateBitmap(String path)
Bitmap myBitmap = BitmapFactory.decodeFile(path);
return ExifUtil.rotateBitmap(path, myBitmap);
新的 ExifUtil.java 文件
package com.makingmemad.apk;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Build;
public class ExifUtil
/**
* @see http://sylvana.net/jpegcrop/exif_orientation.html
*/
public static Bitmap rotateBitmap(String src, Bitmap bitmap)
try
int orientation = getExifOrientation(src);
if (orientation == 1)
return bitmap;
Matrix matrix = new Matrix();
switch (orientation)
case 2:
matrix.setScale(-1, 1);
break;
case 3:
matrix.setRotate(180);
break;
case 4:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case 5:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case 6:
matrix.setRotate(90);
break;
case 7:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case 8:
matrix.setRotate(-90);
break;
default:
return bitmap;
try
Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return oriented;
catch (OutOfMemoryError e)
e.printStackTrace();
return bitmap;
catch (IOException e)
e.printStackTrace();
return bitmap;
private static int getExifOrientation(String src) throws IOException
int orientation = 1;
try
/**
* if your are targeting only api level >= 5
* ExifInterface exif = new ExifInterface(src);
* orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
*/
if (Build.VERSION.SDK_INT >= 5)
Class <? > exifClass = Class.forName("android.media.ExifInterface");
Constructor <? > exifConstructor = exifClass.getConstructor(new Class[]
String.class
);
Object exifInstance = exifConstructor.newInstance(new Object[]
src
);
Method getAttributeInt = exifClass.getMethod("getAttributeInt", new Class[]
String.class, int.class
);
Field tagOrientationField = exifClass.getField("TAG_ORIENTATION");
String tagOrientation = (String) tagOrientationField.get(null);
orientation = (Integer) getAttributeInt.invoke(exifInstance, new Object[]
tagOrientation, 1
);
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SecurityException e)
e.printStackTrace();
catch (NoSuchMethodException e)
e.printStackTrace();
catch (IllegalArgumentException e)
e.printStackTrace();
catch (InstantiationException e)
e.printStackTrace();
catch (IllegalAccessException e)
e.printStackTrace();
catch (InvocationTargetException e)
e.printStackTrace();
catch (NoSuchFieldException e)
e.printStackTrace();
return orientation;
【讨论】:
嗨,Noev,我正在尝试实现此代码,但我不确定将它放在我的主要活动或第二个活动中的哪个位置,我尝试将它放在两个活动中,但它似乎没有改变任何东西。如果你能帮助更好地理解它应该去哪里,那将是最好的!:) 创建一个名为ExifUtil 的单独类,它应该包含rotateBitmap 方法和getExifOrientation。然后在需要旋转位图的任何地方使用 rotateBitmap,例如 imageView.setImageBitmap(rotateBitmap(imgPath)); 嗨 noev 如果你向上看,你可以看到我创建的新 ExifUtil 类,我试图把这个代码 ImageView.setImageBitmap(rotateBitmap(imgPath));在我的 SecondActivity.java 文件中的其他 ImageView imageView 代码的上方和下方,但 imgPath 下总是有一条红线,如果我将 imgPath 更改为文件路径,则 rotateBitmap 变为红色:S 那么,为什么不将鼠标悬停在告诉您问题所在的红线上呢? 当我将 imgPath 更改为文件路径时,它会为我提供 4 个选项,创建局部变量、字段等。无论我选择哪一个,然后 rotateBitmap 会在我检查时变为红色,然后显示创建方法或字符串,当我这样做时,你给我的整行变成红色,并说不能对非静态引用进行静态引用。就像我说的我是菜鸟哈哈:(以上是关于为啥人像视图照片水平显示?的主要内容,如果未能解决你的问题,请参考以下文章
为啥视图没有显示在 android studio 的设计中?