删除SD卡上的文件?

时间:2012-04-12 07:12:42

标签: android sd-card delete-file

我一直在尝试使用以下代码删除SD卡上的文件,但它无法正常工作。  这是显示删除选项的上下文菜单,但是当我按它时没有任何反应,顺便说一下,“pass”是我的应用程序在SD卡上创建的目录:

public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
            super.onCreateContextMenu(menu, v, menuInfo); 
                menu.setHeaderTitle("Context Menu");  
                menu.add(0, v.getId(), 0, "delete");  
                menu.add(0, v.getId(), 0, "wtever");  
            }  


        @Override  
        public boolean onContextItemSelected(MenuItem item) {  
            if(item.getTitle()=="delete"){function1(item.getItemId());}  
            else if(item.getTitle()=="wtever"){function2(item.getItemId());}  
            else {return false;}  
        return true;  
        }  

        public void function1(int id){  
            Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();  
            File folder = Environment.getExternalStorageDirectory();
            String fileName = folder.getPath() + "/pass/hello.pdf";

            File myFile = new File(fileName);
            if(myFile.exists())
                myFile.delete();

        }  

//列表类

 public class PDFListActivity extends ListActivity {
            ArrayAdapter<String> adapter;
            int clickCounter=0;
            ArrayList<String> listItems=new ArrayList<String>();
            private File[] imagelist;
            String[] pdflist;


        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.mainlistpdf);
              File images=new File(Environment.getExternalStorageDirectory(), "pass");
                imagelist = images.listFiles(new FilenameFilter(){  
                        public boolean accept(File dir, String name)  
                        {    

                                return ((name.endsWith(".pdf")));  
                        }  
                });

                pdflist = new String[imagelist.length];
                for(int i = 0;i<imagelist.length;i++)
                {
                        pdflist[i] = imagelist[i].getName();
                }
                this.setListAdapter(new ArrayAdapter<String>(this,
                                android.R.layout.simple_list_item_1, pdflist));
                ListView list=getListView();
                registerForContextMenu(list);
        }

2 个答案:

答案 0 :(得分:1)

使用以下代码从sdcard ::

中删除所有文件和目录
public static boolean deleteDirectory(File path) {
        // TODO Auto-generated method stub
        if( path.exists() ) {
            File[] files = path.listFiles();
            for(int i=0; i<files.length; i++) {
                if(files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                }
                else {
                    files[i].delete();
                }
            }
        }
        return(path.delete());
    }

答案 1 :(得分:1)

使用此LINK创建自定义上下文菜单,并将菜单文件放在res / menu文件夹中。

或尝试替换以下代码

File folder = Environment.getExternalStorageDirectory();
String fileName = folder.getPath() + "/pass/hello.pdf";

 String fileName = Environment.getExternalStorageDirectory() + "/pass/hello.pdf";