简体   繁体   English

旋转屏幕后尝试从活动中接收响应时,Android应用程序崩溃

[英]Android app crashes when trying to receive a response from an activity after the screen has been rotated

I'm writing an application that has a main activity that on creation sends the intent to use the standard camera app. 我正在编写一个具有主要活动的应用程序,该活动在创建时就发送了使用标准相机应用程序的意图。 The camera app takes a picture and returns the result to the main activity, which then decides on the action to perform. 相机应用程序会拍照,并将结果返回到主要活动,然后主要活动决定要执行的动作。 If the screen is rotated while the camera app is on, it fails to return a response to the main activity, otherwise it works flawlessly. 如果在打开相机应用程序的同时旋转了屏幕,则它无法返回对主要活动的响应,否则它将正常工作。 Here follows the code structure 这里遵循代码结构

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);
            }
        }
    }
}

Here's the log 这是日志

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

当屏幕在Create方法上旋转时,所有内容都将重新初始化,因此请确保您再次初始化所有变量并再次说明整个过程。

Rotating the screen will cause the Activity to be destroyed and created again, unless you specify otherwise. 除非另行指定,否则旋转屏幕将导致活动被销毁并再次创建。

To prevent that destruction and re-creation on screen rotate, add android:configChanges="orientation" to the Activitys entry in AndroidManifest. 为防止屏幕上的破坏和重新创建旋转,请将android:configChanges="orientation"到AndroidManifest的android:configChanges="orientation"活动”条目中。 It will look something like this: 它看起来像这样:

    <activity
        android:name="com.mpascual.example.IncallActivity"
        android:configChanges="orientation|keyboardHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>

edit: I just added keyboardHidden to show you how you can set multiple values (in case you need it) 编辑:我刚刚添加了keyboardHidden来向您展示如何设置多个值(以防您需要)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM