2012-08-09 72 views
1

朋友您好,我在我的项目中使用了以下代码。删除SD卡中的图像副本

权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.CAMERA"/> 

XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:text="Camera Test" /> 
    <ImageView android:id="@+id/camera_image" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
</LinearLayout> 

代码(Java文件):

public class ImageInfo extends Activity { 
     private static final int CAMERA_PIC_REQUEST = 1111; 
    private ImageView mImage; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mImage = (ImageView) findViewById(R.id.camera_image); 
     //1 
     Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(intent, CAMERA_PIC_REQUEST); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(resultCode != RESULT_CANCELED) { 
      if(requestCode == CAMERA_PIC_REQUEST){ 
       //2 
       System.out.println("Request"+requestCode+"result"+resultCode); 
       Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
       // mImage.setImageBitmap(thumbnail); 
       //3 
       ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
       thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
       //4 
       File file = new File(Environment.getExternalStorageDirectory()+"/saved_images/image.jpeg"); 
       try { 
        file.createNewFile(); 
        FileOutputStream fo = new FileOutputStream(file); 
        //5 
        fo.write(bytes.toByteArray()); 
        fo.close(); 
        Intent intent=new Intent(this,Info.class); 
        startActivity(intent); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } 
     } 
    } 
} 

我能够成功地操作相机和拍摄图像布坦额外副本正在通过name_date.jpg在DCIM的DCIM目录(deafult目录)中创建图像 请帮助删除所有这些文件。 感谢&问候.....

+0

检查这个问题, http://stackoverflow.com/questions/6390163/deleting-a-gallery-image-after-camera-intent-photo-taken – vipsy 2012-08-09 07:09:38

+0

@onkar。你有解决方案吗? – 2012-08-09 07:18:17

+0

@nick队友试图很难理解,但无法得到它... – onkar 2012-08-09 07:19:45

回答

2

这是从dcim文件夹中删除文件的问题的完整解决方案。

just copy and paste this method. And call it whenever necessary. 
private void deleteLatest() { 
     // TODO Auto-generated method stub 
     File f = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera"); 

     //Log.i("Log", "file name in delete folder : "+f.toString()); 
     File [] files = f.listFiles(); 

     //Log.i("Log", "List of files is: " +files.toString()); 
     Arrays.sort(files, new Comparator<Object>() 
       { 
      public int compare(Object o1, Object o2) { 

       if (((File)o1).lastModified() > ((File)o2).lastModified()) { 
        //   Log.i("Log", "Going -1"); 
        return -1; 
       } else if (((File)o1).lastModified() < ((File)o2).lastModified()) { 
        //  Log.i("Log", "Going +1"); 
        return 1; 
       } else { 
        //  Log.i("Log", "Going 0"); 
        return 0; 
       } 
      } 

       }); 

     //Log.i("Log", "Count of the FILES AFTER DELETING ::"+files[0].length()); 
     files[0].delete(); 

    } 
+0

感谢队友......这工作很好..我想它不会影响存储在Environment.getExternalStorageDirectory()+“/ DCIM中的文件/ Camera“目录... – onkar 2012-08-09 07:35:07

+1

@onkar。很高兴为您效劳。 – 2012-08-09 07:45:58

0

作为连接线程@vipsy,你可以指定

File fileDir = new File(Environment.getExternalStorageDirectory() + 
    "/saved_images"); 
fileDir.mkdirs(); 

File file = new File(fileDir, "image.jpg"); 

Uri outputFileUri = Uri.fromFile(file); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 

这样的文件路径URI相机将图像保存到指定的路径,而不是DCIM文件夹。 您不必手动复制它,也不需要删除任何东西。

编辑:你必须事先创建在SD卡的文件夹,也许这就是poblem。否则,这应该工作。

+0

确定队友会给这个代码试一试 – onkar 2012-08-09 07:21:44

+0

没有队友这个代码不工作..仍然是同一个问题:( – onkar 2012-08-09 07:28:09

+0

你把它放在哪里?你必须 – 2012-08-09 07:29:16

0
File fileOrDirectory= new File(Environment.getExternalStorageDirectory() + 
    "/saved_images"); 

here fileOrDirectory is a path where you are saving images captured by camera . 
you can call this method passing file directory when u need; 



void DeleteRecursive(File fileOrDirectory) { 
     if (fileOrDirectory.isDirectory()) 
      for (File child : fileOrDirectory.listFiles()) 
       DeleteRecursive(child); 

     fileOrDirectory.delete(); 
    }