我正在尝试启动相机活动,然后在用户保存并返回应用时捕获照片。这在运行Android 2.2的手机上工作正常但我在运行4.1.2的平板电脑上遇到了NullPointerException。
当我尝试从传递的'数据'中创建一个位图时,我看到应用程序在'onActivityResult'中获得了NullPointerException(显然没有数据传递):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
pauseTime = System.currentTimeMillis();
Log.i(TAG, "Camera result time pauseTime should be reset: " + pauseTime);
Log.i(TAG, "Camera resultCode is equal to: " + resultCode + " RESULT_OK is: " + RESULT_OK);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
try {
Bitmap photo = (Bitmap) data.getExtras().get("data");
} catch (Exception e) {
// Data is null getting sent back
e.printStackTrace();
}
logcat中的错误如下所示:
07-29 17:16:50.795: W/System.err(19517): java.lang.NullPointerException
07-29 17:16:50.795: W/System.err(19517): at com.android.thoughtfi.UsersThoughts.onActivityResult(UsersThoughts.java:163)
07-29 17:16:50.795: W/System.err(19517): at android.app.Activity.dispatchActivityResult(Activity.java:5391)
07-29 17:16:50.795: W/System.err(19517): at android.app.ActivityThread.deliverResults(ActivityThread.java:3215)
07-29 17:16:50.795: W/System.err(19517): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3262)
07-29 17:16:50.795: W/System.err(19517): at android.app.ActivityThread.access$1200(ActivityThread.java:140)
07-29 17:16:50.795: W/System.err(19517): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
07-29 17:16:50.800: W/System.err(19517): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 17:16:50.800: W/System.err(19517): at android.os.Looper.loop(Looper.java:137)
07-29 17:16:50.800: W/System.err(19517): at android.app.ActivityThread.main(ActivityThread.java:4947)
07-29 17:16:50.800: W/System.err(19517): at java.lang.reflect.Method.invokeNative(Native Method)
为什么没有任何数据返回到应用程序的onactivityresult?
编辑(有关额外信息,这里是photoButton):
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// possible option
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_REQUEST);
}
});
编辑这似乎工作正常,但无论我如何将它放在平板电脑上,我仍然会以横向模式显示照片:
onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
pauseTime = System.currentTimeMillis();
Log.i(TAG, "Camera result time pauseTime should be reset: " + pauseTime);
Log.i(TAG, "Camera resultCode is equal to: " + resultCode + " ");
Log.i(TAG, "The intent mCapturedImageURI was: " + mCapturedImageURI);
Bitmap photo = null;
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
if(data != null){
photo = (Bitmap) data.getExtras().get("data");
} else {
try {
photo = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mCapturedImageURI);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
imageView.setImageBitmap(photo);
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(mCapturedImageURI, projection, null,
null, null);
int column_index_data = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
capturedImageFilePath = cursor.getString(column_index_data);
Photo BUtton:
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// possible option
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_REQUEST);
}
});
答案 0 :(得分:0)
在相机的reference page上,doc说明它返回包含捕获图片的文件的路径。
相关的代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Toast.makeText(this, "Image saved to: " + data.getData(), Toast.LENGTH_LONG).show();
} else {
// whatever
}
}
编辑:来自MediaStore.ACTION_IMAGE_CAPTURE description
可以发送给相机应用程序的标准意图操作 捕获图像并将其返回。
调用者可以传递额外的EXTRA_OUTPUT来控制此图像的位置 将写。如果EXTRA_OUTPUT不存在,则小 size图像作为额外字段中的Bitmap对象返回。这是 适用于只需要小图像的应用程序。如果 存在EXTRA_OUTPUT,然后将写入全尺寸图像 EXTRA_OUTPUT的Uri值。
如果我理解了这一点,Camera活动会返回:
你是否在启动相机的Intent中指定了一个EXTRA_OUTPUT参数?