Simple Android Camera App - Rotation causes NullPo

2019-02-16 00:18发布

I'm new to Android development and was playing around with the camera. I just wanted to create a simple app that would take a photo using the native camera app and give me back the file path of that image.

I have that working fine, but I've hit a strange error. When I tap the button to launch the camera, if I change the orientation of the screen while in the camera app, and don't switch back before I exit the camera (pressing the Done button when I'm asked if I want to retake or not), it causes a NullPointerException to be thrown.

I'm at a bit of a loss here as to how to figure this one out, so any information would be helpful!

Here is the code I have so far:

package com.CameraTest;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CameraTestActivity extends Activity
{
    private static final int CAMERA_PIC_REQUEST = 1234;
    protected Uri mCapturedImageURI;
    protected TextView textview;
    protected Button button;

    /** Called when the activity is first created. */
    @Override public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button) findViewById(R.id.button1);
        textview = (TextView) findViewById(R.id.textView1);

        if (savedInstanceState != null) {
            textview.setText(savedInstanceState.getString("textview"));
        }

        button.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {               
                String fileName = "temp.jpg";  
                ContentValues values = new ContentValues();  
                values.put(MediaStore.Images.Media.TITLE, fileName);  
                mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
                intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);  
                startActivityForResult(intent, CAMERA_PIC_REQUEST);
            }
        });
    }

    @Override protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);

        outState.putString("textview", textview.getText().toString());
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == CAMERA_PIC_REQUEST && resultCode == -1)
        {
            String[] projection = { MediaStore.Images.Media.DATA}; 
            Cursor cursor = managedQuery(this.mCapturedImageURI, projection, null, null, null); 
            int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
            cursor.moveToFirst(); 
            String capturedImageFilePath = cursor.getString(column_index_data);
            this.textview.setText("File Path:" + capturedImageFilePath);
        }
    }    
}

1条回答
看我几分像从前
2楼-- · 2019-02-16 00:37

you have to change your manifest file

in your manifest just replace below code

<activity android:name=".CameraTestActivity"
              android:label="@string/app_name" android:configChanges="keyboardHidden|orientation">

with your code

<activity android:name=".CameraTestActivity"
              android:label="@string/app_name">
查看更多
登录 后发表回答