Android下保存简单网页到本地(包括简单图片链接转换)实现代码
最近在做一个项目涉及到将包含图片的简单网页下载到本地,方便离线时观看,在这里分享一下,大家做下简单修改就可以用到自己的项目中了。(这里用到了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>() {
@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("(?<=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>() {
@override
public 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;
}
}
上一篇: Android创建文件实现对文件监听示例