カメラから戻ってくるときに onActivityResult が呼ばれるんですが、
そのまえに onCreate() が呼ばれている。要するに Activity が再生成されているから、
カメラ呼び出し前にメンバ変数セットしていようとも、もろともいなくなってしまったと。
以前にこんなのを書きました。
Android でカメラかギャラリーから画像を読み込んでトリミングして表示するサンプル
ただここに盲点が有りました。
Samsung Galaxy シリーズという盲点がぁがぁ・・・!!(大げさ)
※2013/01/30 21:33 追記
Google+ でご指摘頂きました。
Activity が破棄され、メンバ変数が初期化されてしまうような場合、
Activity#onSaveInstanceState(Bundle outState) で Bundle に メンバ変数として保持していた値を put() 。
その後復帰後に Activity#onRestoreInstanceState(Bundle savedInstanceState) で Bundle から 取り出して再度メンバに設定してあげるようです。
Activity のライフサイクル大事!!
ご指摘ありがとうございます!!
AndroidManifest にこう書いてます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="me.sotm.practice.camera" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk | |
android:minSdkVersion="10" | |
android:targetSdkVersion="15" /> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | |
<uses-feature android:name="android.hardware.camera" /> | |
<uses-feature android:name="android.hardware.camera.autofocus" /> | |
<application | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name=".MainActivity" | |
android:configChanges="keyboardHidden|orientation" | |
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> | |
</manifest> |
android:configChanges="keyboardHidden|orientation"なにげにこれだけでも大抵動いちゃうんですよね。
HTC J butterfly HTL21 も特に問題ありませんでした。
ここで登場 Samsung Galaxy SIII SC-06D。
いろいろテストしていく中で、写真を取る向きを検証していました 。
そして事は起こった。
この向き!端末の下を左に持ってきて写真を撮る。
この状態だと、configChanges の orientation には引っかからないようです。
他の3方向は問題なし。まあどうするかというと、 screenSize も追加しろと。
ここに書いてありました。
Handling the Configuration Change YourselfAndroid 3.2 (API level 13) 以上を対象とする場合は、screanSize もまた変更されるようです(向き変わってるだけだろ・・・・・)。
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
というわけで Android Manifest にはこう書きます。screenSize 追加。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<activity | |
android:name=".MainActivity" | |
android:configChanges="keyboardHidden|orientation|screenSize" | |
android:screenOrientation="portrait" | |
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> |
ちなみに、 GALAXY SII WiMAX ISW11SC でも同じ現象でした。