我在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度!”。据我所知,三星设备经常会遇到此问题,这很可能就是您所遇到的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句