Search code examples
androidsd-carddelete-file

Deleting files on sdcard?


I've been trying to delete files on sdcard using the following code, but it is not working. Here's the context menu showing the option to delete, but when I press it nothing happens, by the way, "pass" is a directory is created on sdcard by my app:

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();

        }  

// the list class

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

Solution

  • Use this LINK to create custom context menu and place your menus file in res/menu folder.

    or try replacing the below code

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

    with

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