gpt4 book ai didi

android - 为什么我的 Android 应用程序中的图像选择器会自动将纵向图像旋转为横向图像?

转载 作者:行者123 更新时间:2023-11-29 21:15:46 25 4
gpt4 key购买 nike

我在我的 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 header 以告诉任何图像查看者“嘿!请将其旋转 270 度!”。这很可能就是您遇到的情况,因为我知道三星设备经常出现此问题。

关于android - 为什么我的 Android 应用程序中的图像选择器会自动将纵向图像旋转为横向图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21482658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com