0

我正在创建一个应用程序,在该应用程序中单击操作栏项目,相机活动开始,并在捕获并保存图片后,将其显示在主要活动中。

现在,如果我在纵向时从 MainActivity 启动相机,然后在相机 Activity 中切换到横向,拍照,然后单击保存按钮,它会返回到 MainActivity,但图像不显示,这是我猜测是因为主要活动正在重新加载,但我不确定。

即使在方向改变之后,我如何才能保留图像。

MainActivity.java

public boolean onOptionsItemSelected(MenuItem item) {
     switch (item.getItemId()) {
     case R.id.menu_camera: 

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        // file creation for saving video
        Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);  
        if (fileUri != null) {
            targetFile = fileUri.getPath();

            // setting file image name
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);   

            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        }

        return true;

onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            ImageView imgPicture = (ImageView) findViewById(R.id.imgPicture);
            if (targetFile != null) {
                theFile = targetFile;
                imgPicture.setImageBitmap(BitmapFactory.decodeFile(theFile));
            }

        } else if (resultCode == RESULT_CANCELED) {

        } else {

            Toast.makeText(this, "Your picture could not be saved.", Toast.LENGTH_LONG).show();
        }
    }
}

清单.xml

 <uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />

<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.ActionBarTest.MainActivity"
        android:label="@string/app_name" 
        android:launchMode="singleTop">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

1 回答 1

0

也许您可以使用它来知道图像是如何保存的,然后制作位图或基于它的任何东西

public static int getExifRotation(String imgPath) 
    {
        try 
        {
            ExifInterface exif = new ExifInterface(imgPath);
            String rotationAmount = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
            if (!TextUtils.isEmpty(rotationAmount)) 
            {
                int rotationParam = Integer.parseInt(rotationAmount);
                switch (rotationParam) 
                {
                    case ExifInterface.ORIENTATION_NORMAL:
                        return 0;
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        return 90;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        return 180;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        return 270;
                    default:
                        return 0;
                }
            } 
            else 
            {
                return 0;
            }
        }
        catch (Exception ex) 
        {
            return 0;
        }
    }

还要检查

if(bitmap.getWidth() > bitmap.getHeight()){}

这会让你知道图片是否是横向的,因为宽度会大于高度,并使用位图构造函数来旋转矩阵:

Matrix matrix = new Matrix();
matrix.postRotate(90); //or whatever
于 2013-03-05T21:49:20.180 回答