我用过手册
http://developer.android.com/guide/topics/media/camera.html
但我有一个问题.此代码保存图像两次.第一张图片保存在/ sdcard / DCIM / Camera中,第二张图片保存在/ sdcard / PicturesMyCameraApp中.如何删除第一次保存到/ sdcard / DCIM / Camera.
感谢帮助
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.imageButton1:
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri());
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.item1:
/*Intent intent = new Intent(ZodiacActivity.this, AboutActivity.class);
startActivity(intent);*/
break;
case R.id.item2:
this.moveTaskToBack(true);
break;
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode==RESULT_OK) {
Toast.makeText(this, "Ulo?eno do: " + getOutputMediaFileUri().toString(), Toast.LENGTH_LONG).show();
}
else {
return;
}
}
}
public Uri getOutputMediaFileUri(){
return Uri.fromFile(getOutputMediaFile());
}
public File getOutputMediaFile(){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
最佳答案
您只需删除该文件:
String pathToFile = "/sdcard/DCIM/Camera/imagename.jpg";
boolean success = new File(pathToFile).delete();
if(success){
// deleting file succeeded
}else{
// it didn't succeed
}
编辑
在/ sdcard / DCIM /相机中获取最新拍摄的照片:
File[] images = new File("/sdcard/DCIM/Camera").listFiles();
File latestSavedImage = images[0];
for(int i=1; i
if(images[i].lastModified() > latestSavedImage.lastModified()){
latestSavedImage = image;
}
}
注意,代码没有经过测试!