Android下保存简单网页到本地(包括简单图片链接转换)实现代码
本文导语: 最近在做一个项目涉及到将包含图片的简单网页下载到本地,方便离线时观看,在这里分享一下,大家做下简单修改就可以用到自己的项目中了。(这里用到了AQuery库) 代码如下:package com.nekocode.xuedao.utils; import java.io.File;impo...
最近在做一个项目涉及到将包含图片的简单网页下载到本地,方便离线时观看,在这里分享一下,大家做下简单修改就可以用到自己的项目中了。(这里用到了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() {
@Override
public 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("(? 0) {
path = path.replace("\", "_");
path = path.replace("/", "_");
path = path.replace(":", "_");
path = path.replace("*", "_");
path = path.replace("?", "_");
path = path.replace(""", "_");
path = path.replace("", "_");
}
return path;
}
}