我在我的Android应用中使用此代码来启动相机/画廊以获取图像,并将其显示到ImageView中。
当用户选择以景观为方向的图像时,每个图像都可以正常工作,但是当用户选择肖像映像时,将显示图像旋转90度。我不明白为什么..我用Android 4.3
在Galaxy S3上测试我的应用程序我注意到只有在手机拍摄图片时才会出现问题。也许这是S3的问题?
这是我的代码:
private void openImageIntent() {
// Camera.
System.gc();
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
Intent chooserIntent = Intent.createChooser(galleryIntent, "Scegli dove prelevare l'immagine");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
//Log.i("sto lanciando il chooser","vado eh");
startActivityForResult(chooserIntent, 4982);
}
private String selectedImagePath;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 4982) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
//Log.i("immagine",selectedImagePath);
myImageView.setImageBitmap(decodeSampledBitmapFromFile(new File(selectedImagePath)));
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public static Bitmap decodeSampledBitmapFromFile(File file) {
// First decode with inJustDecodeBounds=true to check dimensions
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getAbsolutePath(), o);
//The new size we want to scale to
final int REQUIRED_SIZE=430;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
scale*=2;
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeFile(file.getAbsolutePath(), o2);
}
许多Android设备,如果拍摄图片时将设备保留在肖像中,请不要旋转图像以形成肖像。相反,他们将图像存储在景观中,并在图像中放置一个Exif标头,以告诉任何图像查看器"嘿!请旋转这个270度!"。最有可能的是您遇到的,我知道三星设备经常有这个问题。