我正在开发一个应用程序来捕获图像并将其保存到 sd 卡..但是每次它保存当前文件并删除以前的文件时,我都使用了这个示例 代码:
//打开按钮点击
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
//对于结果
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" + data.getData(),
Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
//保存文件
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
private static File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(type));
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
日志猫
02-28 17:12:49.606: E/AndroidRuntime(4791): FATAL EXCEPTION: main
02-28 17:12:49.606: E/AndroidRuntime(4791): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.captureimage/com.example.captureimage.MainActivity}: java.lang.NullPointerException
02-28 17:12:49.606: E/AndroidRuntime(4791): at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
02-28 17:12:49.606: E/AndroidRuntime(4791): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
02-28 17:12:49.606: E/AndroidRuntime(4791): at android.app.ActivityThread.access$1100(ActivityThread.java:123)
02-28 17:12:49.606: E/AndroidRuntime(4791): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
02-28 17:12:49.606: E/AndroidRuntime(4791): at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 17:12:49.606: E/AndroidRuntime(4791): at android.os.Looper.loop(Looper.java:137)
02-28 17:12:49.606: E/AndroidRuntime(4791): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-28 17:12:49.606: E/AndroidRuntime(4791): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 17:12:49.606: E/AndroidRuntime(4791): at java.lang.reflect.Method.invoke(Method.java:511)
02-28 17:12:49.606: E/AndroidRuntime(4791): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-28 17:12:49.606: E/AndroidRuntime(4791): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-28 17:12:49.606: E/AndroidRuntime(4791): at dalvik.system.NativeStart.main(Native Method)
02-28 17:12:49.606: E/AndroidRuntime(4791): Caused by: java.lang.NullPointerException
02-28 17:12:49.606: E/AndroidRuntime(4791): at com.example.captureimage.MainActivity.onActivityResult(MainActivity.java:140)
02-28 17:12:49.606: E/AndroidRuntime(4791): at android.app.Activity.dispatchActivityResult(Activity.java:4676)
02-28 17:12:49.606: E/AndroidRuntime(4791): at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
它在这条线上:
Toast.makeText(this, "Image saved to:\n" + data.getData(),
Toast.LENGTH_LONG).show();