android - How to delete file from storage -
i have content menu pop menu of rename , delete when press item in few seconds. dont know how correct directory of 1 file. here code:
@override public boolean oncontextitemselected(menuitem item) { adapterview.adaptercontextmenuinfo info = (adapterview.adaptercontextmenuinfo) item.getmenuinfo(); switch(item.getitemid()) { case r.id.rename: // edit stuff here return true; case r.id.delete: file dir = new file(environment.getexternalstoragedirectory()+"/music/musicplayer"); if (dir.isdirectory()) { string[] children = dir.list(); (int = 0; < children.length; i++) { new file(dir, children[i]).delete(); } } // remove stuff here return true; default: return super.oncontextitemselected(item); } }
use below util function either delete file or directory.
public static boolean delete(file path) { boolean result = true; if (path.exists()) { if (path.isdirectory()) { (file child : path.listfiles()) { result &= delete(child); } result &= path.delete(); // delete empty directory. } else if (path.isfile()) { result &= path.delete(); } return result; } else { return false; } }
usage:
file dir = new file(environment.getexternalstoragedirectory()+"/music/musicplayer"); delete(dir);
Comments
Post a Comment