1

当我从图库中选择图像并将其缩放以适合视图时,有时图像显示会自动旋转 -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>

4

1 回答 1

1

消除

m.postRotate(90);

从你的代码和改变

options.inSampleSize = 2; 

代替

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

并删除

android:scaleType="centerCrop" 

从图像视图也希望帮助

于 2012-08-09T09:20:48.533 回答