在android中从gallery中选择大图像后禁用自动旋转
当我从gallery中选择图像并将其缩放以适应视图时,有时图像显示会自动旋转-90度。这很奇怪。 我注意到只有当选定的图像太大时才会发生这种情况。 代码或android可能有什么问题 我使用了以下代码-在android中从gallery中选择大图像后禁用自动旋转,android,bitmap,Android,Bitmap,当我从gallery中选择图像并将其缩放以适应视图时,有时图像显示会自动旋转-90度。这很奇怪。 我注意到只有当选定的图像太大时才会发生这种情况。 代码或android可能有什么问题 我使用了以下代码- public class MainActivity extends Activity { TextView textTargetUri; ImageView targetImage; @Override public void onCreate(Bundle savedInstanceSta
public class MainActivity extends Activity {
TextView textTargetUri;
ImageView targetImage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonLoadImage = (Button)findViewById(R.id.loadimage);
textTargetUri = (TextView)findViewById(R.id.targeturi);
targetImage = (ImageView)findViewById(R.id.targetimage);
buttonLoadImage.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
Uri targetUri = data.getData();
textTargetUri.setText(targetUri.toString());
Toast.makeText(getApplicationContext(),
"ImageView: " + targetImage.getWidth() + " x " + targetImage.getHeight(),
Toast.LENGTH_LONG).show();
Bitmap bitmap;
bitmap = decodeSampledBitmapFromUri(
targetUri,
targetImage.getWidth(), targetImage.getHeight());
if(bitmap == null){
Toast.makeText(getApplicationContext(), "the image data could not be decoded", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),
"Decoded Bitmap: " + bitmap.getWidth() + " x " + bitmap.getHeight(),
Toast.LENGTH_LONG).show();
targetImage.setImageBitmap(bitmap);
}
}
}
/*
* How to "Loading Large Bitmaps Efficiently"?
* Refer: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
*/
public Bitmap decodeSampledBitmapFromUri(Uri uri, int reqWidth, int reqHeight) {
Bitmap bm = null;
try{
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
return bm;
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
}
移除
m.postRotate(90);
从你的代码
改变
options.inSampleSize = 2;
而不是
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
移除
android:scaleType="centerCrop"
也可以从图像视图
希望有帮助删除
m.postRotate(90);
从你的代码
改变
options.inSampleSize = 2;
而不是
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
移除
android:scaleType="centerCrop"
也可以从图像视图
希望获得帮助发布代码或将活动屏幕方向设置为“尝试选项”。inSampleSize=2;而不是options.inSampleSize=计算样本大小(选项、reqWidth、reqHeight)@DeepakSwami-我确实根据您的要求进行了更改,但同样的问题您是否可以粘贴您的活动?\u main.xml和manifest.xml?我也有同样的问题,我认为问题只会发生在大型设备上。仍然无法获得解决方案。发布代码或将活动屏幕方向设置为“尝试选项”。inSampleSize=2;而不是options.inSampleSize=计算样本大小(选项、reqWidth、reqHeight)@DeepakSwami-我确实根据您的要求进行了更改,但同样的问题您是否可以粘贴您的活动?\u main.xml和manifest.xml?我也有同样的问题,我认为问题只会发生在大型设备上。仍然没有得到解决方案。