我是新来的Android开发,并用相机玩耍。 我只是想创建一个简单的应用程序,会使用本机相机应用拍摄照片,并给我回该图像的文件路径。
我有工作正常,但我已经打了一个奇怪的错误。 当我点击按钮来启动相机,如果我改变屏幕的方向,而在摄像头的应用程序,并且不切换回之前,我退出相机(按完成按钮时,我问我是否想夺回或没有),它会导致抛出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);
}
}
}