我正在编写一个具有主要活动的应用程序,该活动在创建时发送使用标准相机应用程序的意图。相机应用程序拍摄照片并将结果返回主活动,然后主要活动决定要执行的操作。如果在相机应用程序打开时旋转屏幕,则无法返回对主活动的响应,否则它将完美无瑕地运行。以下是代码结构
public class MainActivity extends Activity {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
private Uri fileUri;
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
<do stuff>
// Open the image
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, "image/*");
startActivity(intent);
}
}
}
}
这是日志
07-14 19:11:10.768: E/BitmapFactory(22377): Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/MyApp/IMG_20140714_191110.jpg: open failed: ENOENT (No such file or directory)
07-14 19:11:10.778: E/AndroidRuntime(22377): FATAL EXCEPTION: main
07-14 19:11:10.778: E/AndroidRuntime(22377): Process: com.domain.myapp, PID: 22377
07-14 19:11:10.778: E/AndroidRuntime(22377): java.lang.RuntimeException: Unable to resume activity {com.domain.myapp/com.domain.myapp.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.domain.myapp/com.domain.myapp.MainActivity}: java.lang.NullPointerException
07-14 19:11:10.778: E/AndroidRuntime(22377): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2774)
07-14 19:11:10.778: E/AndroidRuntime(22377): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
07-14 19:11:10.778: E/AndroidRuntime(22377): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
07-14 19:11:10.778: E/AndroidRuntime(22377): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3724)
07-14 19:11:10.778: E/AndroidRuntime(22377): at android.app.ActivityThread.access$900(ActivityThread.java:135)
07-14 19:11:10.778: E/AndroidRuntime(22377): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
07-14 19:11:10.778: E/AndroidRuntime(22377): at android.os.Handler.dispatchMessage(Handler.java:102)
07-14 19:11:10.778: E/AndroidRuntime(22377): at android.os.Looper.loop(Looper.java:136)
07-14 19:11:10.778: E/AndroidRuntime(22377): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-14 19:11:10.778: E/AndroidRuntime(22377): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 19:11:10.778: E/AndroidRuntime(22377): at java.lang.reflect.Method.invoke(Method.java:515)
07-14 19:11:10.778: E/AndroidRuntime(22377): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-14 19:11:10.778: E/AndroidRuntime(22377): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-14 19:11:10.778: E/AndroidRuntime(22377): at dalvik.system.NativeStart.main(Native Method)
07-14 19:11:10.778: E/AndroidRuntime(22377): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.domain.myapp/com.domain.myapp.MainActivity}: java.lang.NullPointerException
07-14 19:11:10.778: E/AndroidRuntime(22377): at android.app.ActivityThread.deliverResults(ActivityThread.java:3351)
07-14 19:11:10.778: E/AndroidRuntime(22377): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2761)
07-14 19:11:10.778: E/AndroidRuntime(22377): ... 13 more
07-14 19:11:10.778: E/AndroidRuntime(22377): Caused by: java.lang.NullPointerException
07-14 19:11:10.778: E/AndroidRuntime(22377): at com.domain.myapp.MainActivity.draw_interface(MainActivity.java:97)
07-14 19:11:10.778: E/AndroidRuntime(22377): at com.domain.myapp.MainActivity.draw(MainActivity.java:188)
07-14 19:11:10.778: E/AndroidRuntime(22377): at com.domain.myapp.MainActivity.onActivityResult(MainActivity.java:234)
07-14 19:11:10.778: E/AndroidRuntime(22377): at android.app.Activity.dispatchActivityResult(Activity.java:5423)
07-14 19:11:10.778: E/AndroidRuntime(22377): at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
07-14 19:11:10.778: E/AndroidRuntime(22377): ... 14 more
07-14 19:13:08.278: E/BitmapFactory(23317): Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/myapp/IMG_20140714_191308.jpg: open failed: ENOENT (No such file or directory)
07-14 19:13:08.278: E/AndroidRuntime(23317): FATAL EXCEPTION: main
07-14 19:13:08.278: E/AndroidRuntime(23317): Process: com.domain.myapp, PID: 23317
07-14 19:13:08.278: E/AndroidRuntime(23317): java.lang.RuntimeException: Unable to resume activity {com.domain.myapp/com.domain.myapp.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.domain.myapp/com.domain.myapp.MainActivity}: java.lang.NullPointerException
07-14 19:13:08.278: E/AndroidRuntime(23317): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2774)
07-14 19:13:08.278: E/AndroidRuntime(23317): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
07-14 19:13:08.278: E/AndroidRuntime(23317): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
07-14 19:13:08.278: E/AndroidRuntime(23317): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3724)
07-14 19:13:08.278: E/AndroidRuntime(23317): at android.app.ActivityThread.access$900(ActivityThread.java:135)
07-14 19:13:08.278: E/AndroidRuntime(23317): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
07-14 19:13:08.278: E/AndroidRuntime(23317): at android.os.Handler.dispatchMessage(Handler.java:102)
07-14 19:13:08.278: E/AndroidRuntime(23317): at android.os.Looper.loop(Looper.java:136)
07-14 19:13:08.278: E/AndroidRuntime(23317): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-14 19:13:08.278: E/AndroidRuntime(23317): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 19:13:08.278: E/AndroidRuntime(23317): at java.lang.reflect.Method.invoke(Method.java:515)
07-14 19:13:08.278: E/AndroidRuntime(23317): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-14 19:13:08.278: E/AndroidRuntime(23317): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-14 19:13:08.278: E/AndroidRuntime(23317): at dalvik.system.NativeStart.main(Native Method)
07-14 19:13:08.278: E/AndroidRuntime(23317): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.domain.myapp/com.domain.myapp.MainActivity}: java.lang.NullPointerException
07-14 19:13:08.278: E/AndroidRuntime(23317): at android.app.ActivityThread.deliverResults(ActivityThread.java:3351)
07-14 19:13:08.278: E/AndroidRuntime(23317): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2761)
07-14 19:13:08.278: E/AndroidRuntime(23317): ... 13 more
07-14 19:13:08.278: E/AndroidRuntime(23317): Caused by: java.lang.NullPointerException
07-14 19:13:08.278: E/AndroidRuntime(23317): at com.domain.myapp.MainActivity.draw_interface(MainActivity.java:97)
07-14 19:13:08.278: E/AndroidRuntime(23317): at com.domain.myapp.MainActivity.draw(MainActivity.java:188)
07-14 19:13:08.278: E/AndroidRuntime(23317): at com.domain.myapp.MainActivity.onActivityResult(MainActivity.java:234)
07-14 19:13:08.278: E/AndroidRuntime(23317): at android.app.Activity.dispatchActivityResult(Activity.java:5423)
07-14 19:13:08.278: E/AndroidRuntime(23317): at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
07-14 19:13:08.278: E/AndroidRuntime(23317): ... 14 more
答案 0 :(得分:0)
当屏幕旋转时,Create方法被调用,所有内容都只是重新初始化,所以请确保再次初始化所有变量并再次说明整个过程。
答案 1 :(得分:0)
除非另有指定,否则旋转屏幕将导致活动被销毁并再次创建。
要防止屏幕上的销毁和重新创建,请将android:configChanges="orientation"
添加到AndroidManifest中的“活动”条目中。它看起来像这样:
<activity
android:name="com.mpascual.example.IncallActivity"
android:configChanges="orientation|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
编辑:我刚刚添加了keyboardHidden
,向您展示如何设置多个值(如果您需要)