android - 简单的 Android 相机应用程序 - 旋转导致 NullPointerException

标签 android camera nullpointerexception android-orientation

我是 Android 开发的新手,正在摆弄相机。我只是想创建一个简单的应用程序,它可以使用 native 相机应用程序拍照并返回该图像的文件路径。

我可以正常工作,但我遇到了一个奇怪的错误。当我点击按钮启动相机时,如果我在相机应用程序中更改屏幕方向,并且在退出相机之前没有切换回来(当我被问及是否要重新拍摄时按完成按钮或不),它会导致抛出 NullPointerException。

对于如何解决这个问题,我有点不知所措,所以任何信息都会有所帮助!

这是我目前的代码:

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

最佳答案

你必须改变你的 list 文件

在你的 list 中只需替换下面的代码

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

用你的代码

<activity android:name=".CameraTestActivity"
              android:label="@string/app_name">

关于android - 简单的 Android 相机应用程序 - 旋转导致 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11065574/

相关文章:

android - 如何在Android内核中公开API

android - 当用户点击它时在 android 中显示更多文本

iphone - 使用ARKit的ARSCNView时,可以使用iPhone的前置摄像头吗?

camera - 我如何根据角色的相机旋转角色? (罗布乐思)

java - Android:无法使用 .getStringExtra 获取值

android - chrome 模拟设备的 Oneplus 6 视口(viewport)大小

android - 如何在 Android 中创建具有动态行数的表

android - 从 Android 上的相机保存图像中的 strip

java - 当我的 Integer[] 已经初始化时,为什么会出现 NullPointerException?

java - 使用 @Autowired 进行 Spring 对象注入(inject)时抛出 NullPointerException