zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android下保存简单网页到本地(包括简单图片链接转换)实现代码

Android转换网页代码 实现 简单 本地 保存
2023-06-13 09:15:17 时间

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

复制代码代码如下:


packagecom.nekocode.xuedao.utils;

importjava.io.File;
importjava.io.FileOutputStream;
importjava.util.ArrayList;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;

importandroid.content.ContentValues;
importandroid.content.Context;
importandroid.database.Cursor;
importandroid.database.sqlite.SQLiteDatabase;

importcom.androidquery.AQuery;
importcom.androidquery.callback.AjaxCallback;
importcom.androidquery.callback.AjaxStatus;
importcom.nekocode.xuedao.PublicData;
importcom.nekocode.xuedao.PublicData.Subscribe;

publicclassHtmlStorageHelper{
 privateStringURL="http://eduproject.sinaapp.com/fetchurl.php/getcontent/";
 privatePublicDatapd;
 privateAQueryaq;
 privateSQLiteDatabasemDB;
 privateStringmDownloadPath;

 publicHtmlStorageHelper(Contextcontext){
  pd=PublicData.getInstance();
  aq=newAQuery(context);
  mDB=context.openOrCreateDatabase("data.db",Context.MODE_PRIVATE,null);
  mDB.execSQL("createtableifnotexistsdownload_html(_idINTEGERPRIMARYKEYAUTOINCREMENT,content_idTEXTNOTNULL,titleTEXTNOTNULL)");

  mDownloadPath=pd.mAppPath+"download/";
  Filedir_file=newFile(pd.mAppPath+"download/");
  if(!dir_file.exists())
   dir_file.mkdir();
 }

 publicvoidsaveHtml(finalStringid,finalStringtitle){
  if(isHtmlSaved(id))
   return;

  aq.ajax(URL+id,String.class,newAjaxCallback<String>(){
   @Override
   publicvoidcallback(Stringurl,Stringhtml,AjaxStatusstatus){
    Filedir_file=newFile(mDownloadPath+id);
    if(!dir_file.exists())
     dir_file.mkdir();

    Patternpattern=Pattern.compile("(?<=src=\")[^\"]+(?=\")");
    Matchermatcher=pattern.matcher(html);
    StringBuffersb=newStringBuffer();
    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);
   }
  });
 }

 privatevoiddownloadPic(Stringid,Stringurl){
  Filepic_file=newFile(mDownloadPath+id+"/"+formatPath(url));
  aq.download(url,pic_file,newAjaxCallback<File>(){
   @Override
   publicvoidcallback(Stringurl,finalFilefile,AjaxStatusstatus){
   }
  });
 }

 privatevoidwriteHtml(Stringid,Stringtitle,Stringhtml){
  Filehtml_file=newFile(mDownloadPath+id+"/index.html");
  FileOutputStreamfos=null;
  try{
   fos=newFileOutputStream(html_file);
           fos.write(html.getBytes());
       }catch(Exceptione){
           e.printStackTrace();
       }finally{
           try{
               fos.close();
           }catch(Exceptione2){
               e2.printStackTrace();
           }
       }

  ContentValuesvalues=newContentValues();
  values.put("content_id",id);
  values.put("title",title);
  mDB.insert("download_html","_id",values);
 }

 publicbooleanisHtmlSaved(Stringid){
  Filefile=newFile(mDownloadPath+id);
  if(file.exists()){
   file=newFile(mDownloadPath+id+"/index.html");
   if(file.exists())
    returntrue;
  }
  deleteHtml(id);
  returnfalse;
 }

 publicStringgetTitle(Stringid){
  Cursorc=mDB.rawQuery("select*fromdownload_htmlwherecontent_id=?",newString[]{id});
  if(c.getCount()==0)
   returnnull;

  c.moveToFirst();
  intindex1=c.getColumnIndex("title");

  returnc.getString(index1);
 }

 publicArrayList<Subscribe>getHtmlList(){
  Cursorc=mDB.rawQuery("select*fromdownload_html",null);
  ArrayList<Subscribe>list=newArrayList<Subscribe>();
  if(c.getCount()!=0){
   c.moveToFirst();
   intindex1=c.getColumnIndex("content_id");
   intindex2=c.getColumnIndex("title");

   while(!c.isAfterLast()){
    Stringid=c.getString(index1);
    if(isHtmlSaved(id)){
     Subscribesub=newSubscribe(
       id,
       c.getString(index2),
       Subscribe.FILE_DOWNLOADED
       );
     list.add(sub);
    }

    c.moveToNext();
   }
  }

  returnlist;
 }

 publicvoiddeleteHtml(Stringid){
  mDB.delete("download_html","content_id=?",newString[]{id});
  Filedir_file=newFile(mDownloadPath+id);
  deleteFile(dir_file);
 }

 privatevoiddeleteFile(Filefile){
  if(file.exists()){//判断文件是否存在
   if(file.isFile()){//判断是否是文件
    file.delete();//delete()方法你应该知道是删除的意思;
   }elseif(file.isDirectory()){//否则如果它是一个目录
    Filefiles[]=file.listFiles();//声明目录下所有的文件files[];
    for(inti=0;i<files.length;i++){//遍历目录下所有的文件
     this.deleteFile(files[i]);//把每个文件用这个方法进行迭代
    }
   }
   file.delete();
  }else{
   //
  }
 }

 privateStringformatPath(Stringpath){
       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(">","_");
       }
       returnpath;
   }
}