Recently in doing a project involves a simple web page containing pictures to download to the local, convenient to watch when offline, share here, you can make a simple change to use their own projects. (The Aquery library is used here.)
Copy Code code as follows:
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 {
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, tit le-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 ()) {//Determine if the file exists
if (File.isfile ()) {//To determine if it is a file
File.delete (); Delete () method you should know is the meaning of deletion;
else if (file.isdirectory ()) {//Otherwise if it is a directory
File files[] = File.listfiles (); All documents under the Declaration directory files[];
for (int i = 0; i < files.length i++) {//Traverse all files under directory
This.deletefile (Files[i]); Each file is iterated using this method
}
}
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;
}
}