【发布时间】:2016-05-17 18:17:28
【问题描述】:
我正在尝试让我的相机为我的 android 应用程序工作,但我不断收到以下错误
02-07 22:30:48.217 13197-13197/com.example.romsm.lap E/AndroidRuntime: 致命例外:主要 java.lang.RuntimeException:传递结果失败 ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/8557(有 额外)}}活动 {com.example.romsm.lap/com.example.romsm.lap.TreeQuestionsActivity}: java.lang.NullPointerException 在 android.app.ActivityThread.deliverResults(ActivityThread.java:3510) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3553) 在 android.app.ActivityThread.access$1200(ActivityThread.java:165) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1374) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:137) 在 android.app.ActivityThread.main(ActivityThread.java:5455) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:525) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 在 dalvik.system.NativeStart.main(Native Method)
我已经尝试了所有可以在网上找到的方法,但似乎没有任何解决方法。 这是我做相机工作的代码。
btnPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// File file = new File(...);
startActivityForResult(takePictureIntent, ACTIVITY_START_CAMERA);
// takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
}
}
});
}
和
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ACTIVITY_START_CAMERA) {
if (data != null && resultCode == RESULT_OK) {
// Image captured and saved to file
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
} else {
// User cancelled the image capture or
// image capture failed, advise user
}
}
super.onActivityResult(requestCode, resultCode, data);
}
【问题讨论】:
-
试用
Uri capturedImageUri = data.getData(); -
为什么需要
Bundle extras = data.getExtras();? -
@JohnJoe 我正在学习一个教程,这就是他们所使用的
标签: android android-camera android-camera-intent