2

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

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);
        }
4

2 回答 2

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());
    }
于 2012-04-12T07:22:49.950 回答
1

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

或尝试替换以下代码

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

 String fileName = Environment.getExternalStorageDirectory() + "/pass/hello.pdf";
于 2012-04-12T07:41:09.183 回答