1

カメラインテントを呼び出した後にイメージビューを更新しようとしてonActivityResultいますが、onActivityResult の後にビューが破棄されて再作成されているようです。onCreateで実行するとうまくいきます。テストにはGalaxy S3を使用しています。

奇妙なことに、デスクトップで同じコードをビルドしたとき、しばらくの間は機能していたようですが、ラップトップでは機能していませんでした。しばらくデバッグした後、デスクトップ ビルドでも問題が再び発生し、デバッガーでは onactivityresult でビットマップが正しく更新されますが、アクティビティが破棄されてすぐに再度作成され、ビューが初期状態にリセットされます。

onCreate は 3 回呼び出されます。1. 画面が最初に表示されるとき、2. カメラ インテントが終了した後、onActivityResult の前、3. on Activity Result の後です。#2 と #3 の前にアクティビティが破棄されて再作成される理由がわかりません。

向きの変更は行っていません。

**コード**

マニフェストのアクティビティ設定

   android:configChanges="orientation"
   android:screenOrientation="portrait" 

アクティビティ

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        if (resultCode == RESULT_OK) {
            String imageUri = "file:///storage/sdcard0/Pictures/Funhouse/IMG_20130826_163938.jpg";

            // Try by using setImageURI
            preview.setImageURI(Uri.parse(imageUri));

        }
     }

onCreate および Intent リクエスト

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        preview = (ImageView) findViewById(R.id.img_preview);

//      works fine if I set the image view here
//      String imageUri = "file:///storage/sdcard0/Pictures/Funhouse/IMG_20130826_163938.jpg";
//      preview.setImageURI(Uri.parse(imageUri));
    }



    public void takePhoto(View view) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 0);

    }

レイアウトファイルはこちら

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

     <Button
         android:id="@+id/btn_take_photo"
         android:layout_width="match_parent"
         android:layout_height="50dp"
         android:layout_marginBottom="67dp"
         android:onClick="takePhoto"
         android:text="Take Photo" />

    <ImageView
        android:id="@+id/img_preview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

 </LinearLayout>
4

1 に答える 1

3

そのため、カメラ アクティビティから戻ると、UI が OnActivityResult で更新された後に on destroy が呼び出され、元の呼び出しアクティビティの UI がリセットされたことがわかりました。画面解像度の変更は、カメラ アクティビティが返されることによってトリガーされていましたが、onActivityResult イベントの後でのみ発生していました。

そのアクティビティの画面解像度の変更を無視するように Android マニフェストを更新すると、修正されました。

 android:configChanges="orientation|screenSize"
 android:screenOrientation="portrait" 
于 2013-08-28T17:18:37.430 に答える