最近在做一个项目涉及到将包含图片的简单网页下载到本地,方便离线时观看,在这里分享一下,大家做下简单修改就可以用到自己的项目中了。(这里用到了AQuery库)


package com.nekocode.xuedao.utils;import java.io.File;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.regex.Matcher;import java.util.regex.Pattern;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import com.androidquery.AQuery;import com.androidquery.callback.AjaxCallback;import com.androidquery.callback.AjaxStatus;import com.nekocode.xuedao.PublicData;import com.nekocode.xuedao.PublicData.Subscribe;public class HtmlStorageHelper {private String URL = "http://eduproject.sinaapp.com/fetchurl.php/getcontent/";private PublicData pd;private AQuery aq;private SQLiteDatabase mDB;private String mDownloadPath;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)");mDownloadPath = pd.mAppPath + "download/";File dir_file = new File(pd.mAppPath + "download/");if(!dir_file.exists())dir_file.mkdir();}public void saveHtml(final String id, final String title) {if(isHtmlSaved(id))return;aq.ajax(URL+id, String.class, new AjaxCallback<String>() {@Overridepublic void callback(String url, String html, AjaxStatus status) {File dir_file = new File(mDownloadPath + id);if(!dir_file.exists())dir_file.mkdir();Pattern pattern = Pattern.compile("(?<=src=\")[^\"]+(?=\")");Matcher matcher = pattern.matcher(html);StringBuffer sb = new StringBuffer(); while(matcher.find()){downloadPic(id, matcher.group(0));matcher.appendReplacement(sb, formatPath(matcher.group(0))); }matcher.appendTail(sb);html = sb.toString();writeHtml(id, title, html);}});}private void downloadPic(String id, String url) {File pic_file = new File(mDownloadPath + id + "/" + formatPath(url));aq.download(url, pic_file, new AjaxCallback<File>() {@Overridepublic void callback(String url, final File file, AjaxStatus status) {}});}private void writeHtml(String id, String title, String html) {File html_file = new File(mDownloadPath + id + "/index.html");FileOutputStream fos = null;try {fos=new FileOutputStream(html_file);            fos.write(html.getBytes());        } catch (Exception e) {            e.printStackTrace();        }finally{            try {                fos.close();            } catch (Exception e2) {                e2.printStackTrace();            }        }ContentValues values = new ContentValues();values.put("content_id", id);values.put("title", title);mDB.insert("download_html", "_id", values);}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 {//}}private String formatPath(String path) {        if (path != null && path.length() > 0) {            path = path.replace("\\", "_");            path = path.replace("/", "_");            path = path.replace(":", "_");            path = path.replace("*", "_");            path = path.replace("?", "_");            path = path.replace("\"", "_");            path = path.replace("<", "_");            path = path.replace("|", "_");            path = path.replace(">", "_");        }        return path;    }}



更多相关文章

  1. Android(安卓)Studio For Mac 启动模拟机时关于“Intel HAXM is
  2. 模拟实现android 文件管理器功能(循环打开文件夹)
  3. 自定义Dialog之信息提示
  4. android环境搭建——工欲善其事,必先利其器 2
  5. Mars视频笔记——Animation(2)
  6. Hello Android(安卓)- File文件操作
  7. Ubuntu14.04 下面安装SVN服务器
  8. android部分目录分析
  9. 【Android】更改程序图标

随机推荐

  1. jQuery 1.3 API 参考文档中文版
  2. [置顶] JQuery datatables 使用Fi
  3. 随着窗口变大,div的左边距也拉大
  4. 重写ajax方法实现异步请求session过期时
  5. 使用jQuery.ajax post函数将javascript数
  6. [置顶] jquery学习之路 初识jq
  7. jQuery在元素内部检测mousedown,然后在元
  8. Jquery实现表格行的添加、删除
  9. 在Access中利用Jquery技术实现专业的界面
  10. 如何使用特定样式获取jquery中的第一个和