我正在尝试通过 Android 模拟器捕获和保存图像,正在捕获图像,但正在保存的文件已损坏。
这可能是什么原因造成的?谁能帮我找出可能的错误点?
下面是我的代码:
**public void onCreate**(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
**Intent** intent = new **Intent**("android.media.action.IMAGE_CAPTURE");
try {
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
intent.putExtra(**MediaStore**.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
@Override
**protected void onActivityResult**(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
StoreImage(this, data.getData(),
mediaFile);
finish();
}
else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
}
else {
finish();
try {
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
堆栈跟踪如下::
04-05 23:55:40.369: E/AndroidRuntime(534): FATAL EXCEPTION: main
04-05 23:55:40.369: E/AndroidRuntime(534): java.lang.RuntimeException: Unable to start activity ComponentInfo{camera.android/camera.android.CameraActivity}: java.lang.NullPointerException: file
04-05 23:55:40.369: E/AndroidRuntime(534): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
04-05 23:55:40.369: E/AndroidRuntime(534): Caused by: java.lang.NullPointerException: file
04-05 23:55:40.369: E/AndroidRuntime(534): at android.net.Uri.fromFile(Uri.java:441)
04-05 23:55:40.369: E/AndroidRuntime(534): at camera.android.CameraActivity.getOutputMediaFileUri(CameraActivity.java:72)
04-05 23:55:40.369: E/AndroidRuntime(534): at camera.android.CameraActivity.onCreate(CameraActivity.java:34)
04-05 23:55:40.369: E/AndroidRuntime(534): at android.app.Activity.performCreate(Activity.java:4465)
04-05 23:55:40.369: E/AndroidRuntime(534): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-05 23:55:40.369: E/AndroidRuntime(534): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)