问题描述
我是Android开发的新手,并且正在与相机一起玩.我只是想创建一个简单的应用程序,该应用程序可以使用本机相机应用程序拍摄照片,并将该图像的文件路径还给我.
我的工作正常,但是我遇到了一个奇怪的错误.当我点击按钮启动相机时,如果在相机应用程序中更改屏幕的方向,并且在退出相机之前不要切换(当我询问是否要重新播放时按DONE按钮是否),导致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); } } }
推荐答案
您必须更改清单文件
在您的清单中,只需替换下面的代码
<activity android:name=".CameraTestActivity" android:label="@string/app_name" android:configChanges="keyboardHidden|orientation">
使用您的代码
<activity android:name=".CameraTestActivity" android:label="@string/app_name">
问题描述
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); } } }
推荐答案
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">