删除路径DCIM

时间:2018-02-25 17:28:33

标签: android

我需要删除Android中的路径DCIM内部 我有这个代码

    File dir = new File("root path");
    FileUtils.deleteDirectory(dir);

但FileUtils不起作用 有人可以帮我写出正确的代码吗?

2 个答案:

答案 0 :(得分:0)

首先,您无法删除DCIM文件夹,因为它是一个系统文件夹,但您可以使用以下方法删除其内容。

public static boolean deleteDCIM(File dir) {

    String [] children = dir.list();
    for ( int i = 0 ; i < children.length ; i ++ )
    {
     File child =    new File( dir , children[i] );
     //if the child is a directory make a recursive call
     if(child.isDirectory()){
         deleteDCIM( child );
     }
     //else delete the child file 
     else{
    child.delete();
     }
    }
}

使用

获取路径
String dir= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

答案 1 :(得分:0)

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    File dir= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    deleteDCIM(dir);
}

public static void deleteDCIM(File dir) {
    String[] children = dir.list();
    for (int i = 0; i < children.length; i++) {
        File child = new File(dir, children[i]);
        //if the child is a directory make a recursive call
        if (child.isDirectory()) {
            deleteDCIM(child);
        }
        //else delete the child file
        else {
            child.delete();
        }
    }
}}