gpt4 book ai didi

android - 在android中从图库中选择大图像后禁用自动旋转

转载 作者:行者123 更新时间:2023-11-30 04:04:50 24 4
gpt4 key购买 nike

当我从图库中选择图像并将其缩放以适合 View 时,有时图像显示会自动旋转 -90 度。这很奇怪。而且我注意到只有当所选图像太大时才会发生这种情况。代码或 android 可能有什么问题?

我使用了以下代码-

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;
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<Button
android:id="@+id/loadimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load Image" />

<TextView
android:id="@+id/targeturi"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/targetimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop" />

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidselectimage"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:screenOrientation="portrait"
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

最佳答案

删除

m.postRotate(90);

来自您的代码和改变

options.inSampleSize = 2; 

代替

options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

并删除

android:scaleType="centerCrop" 

也从 ImageView 希望帮助

关于android - 在android中从图库中选择大图像后禁用自动旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11879697/

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