我正在開發一個應用程序來捕捉圖像並將其保存到SD卡..但每次它保存當前文件並刪除以前的一個我已經使用這個example 代碼中使用:使用意圖的圖像捕捉應用程序只保存當前圖片..previouse圖片每次丟失
//在按鈕單擊
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;
}
logcat的
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();
您的意思是如何刪除前一個?它可能會覆蓋它嗎?在製作第二張照片時,你確定你的時間戳是不同的嗎? – 2013-02-28 11:36:51
是的,我每次都得到相同的時間戳,所以它覆蓋了前一個,但我使用的日期和時間,所以它應該給每次不同的時間戳? – Shiv 2013-02-28 11:39:09
你如何檢查你的文件的存在。一些文件瀏覽器應用程序? – 2013-02-28 11:40:58