public HtmlStorageHelper(Context context) { pd = PublicData.getInstance(); aq = new AQuery(context); mDB = context.openOrCreateDatabase("data.db", Context.MODE_PRIVATE, null); mDB.execSQL("create table if not exists download_html(_id INTEGER PRIMARY KEY AUTOINCREMENT, content_id TEXT NOT NULL, title TEXT NOT NULL)");
public boolean isHtmlSaved(String id) { File file = new File(mDownloadPath + id); if(file.exists()) { file = new File(mDownloadPath + id + "/index.html"); if(file.exists()) return true; } deleteHtml(id); return false; }
public String getTitle(String id) { Cursor c = mDB.rawQuery("select * from download_html where content_id=?", new String[]{id}); if(c.getCount() == 0) return null;
c.moveToFirst(); int index1 = c.getColumnIndex("title");
return c.getString(index1); }
public ArrayList<Subscribe> getHtmlList() { Cursor c = mDB.rawQuery("select * from download_html", null); ArrayList<Subscribe> list = new ArrayList<Subscribe>(); if(c.getCount() != 0) { c.moveToFirst(); int index1 = c.getColumnIndex("content_id"); int index2 = c.getColumnIndex("title");
while (!c.isAfterLast()) { String id = c.getString(index1); if(isHtmlSaved(id)) { Subscribe sub = new Subscribe( id, c.getString(index2), Subscribe.FILE_DOWNLOADED ); list.add(sub); }
c.moveToNext(); } }
return list; }
public void deleteHtml(String id) { mDB.delete("download_html", "content_id=?", new String[]{id}); File dir_file = new File(mDownloadPath + id); deleteFile(dir_file); } private void deleteFile(File file) { if (file.exists()) { // 判断文件是否存在 if (file.isFile()) { // 判断是否是文件 file.delete(); // delete()方法 你应该知道 是删除的意思; } else if (file.isDirectory()) { // 否则如果它是一个目录 File files[] = file.listFiles(); // 声明目录下所有的文件 files[]; for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件 this.deleteFile(files[i]); // 把每个文件 用这个方法进行迭代 } } file.delete(); } else { // } }