0

ユーザーがWebページを表示するためにインターネット接続を必要としないように、Androidプロジェクトフォルダー内にWebページを保存したいと考えています。私はアンドロイドのウェブビューを使用しています。HTTP プロトコルで Web ページを表示できます。私のコードは以下の通りです:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        WebView webview = (WebView) findViewById(R.id.webView1);
       // webview.loadUrl("http://www.mysite.com/index.html");

        webview.getSettings().setJavaScriptEnabled(true);
    }

しかし、ウェブページをオフラインで見たいです。インターネットに接続していなくても、WebページをAndroidプロジェクトフォルダーにリソースとして保存して表示できる方法はありますか?

4

2 に答える 2

1

はい !

それらを/assetsフォルダーに入れて、次のようにアクセスします。

webview.loadUrl("file:///android_asset/my_html_page.html"); 

この質問はすでに回答されています: Webview load html from assets ディレクトリ

于 2012-09-05T12:36:42.077 に答える
0

ウェブページを Asset フォルダーに保存して使用する

         public  File getfile(String filename) throws IOException {
    // TODO Auto-generated method stub
   String externalStorage_path          =Environment.getExternalStorageDirectory().toString();
   String state = Environment.getExternalStorageState();
   if (Environment.MEDIA_MOUNTED.equals(state)){
        File dir = new File(externalStorage_path + "/yourfilename");
        dir.mkdir();
        File mfile = new File(dir,filename);
        if( mfile.exists()==true)  return  mfile;
        else{
              try{
                  InputStream myInput =  mcontext.getAssets().open(filename);
                  String path =externalStorage_path+"/yourfilename";
                  OutputStream  myOutput = new FileOutputStream (path);
                  byte[] buffer = new byte[1024];
                  int length;
                  try {
                      while((length = myInput.read(buffer))>0)
                      myOutput.write(buffer,0,length);
                  }catch(FileNotFoundException e){Log.d("error",""+ e.toString());
                  }finally{
                      myOutput.flush();
                      myOutput.close();
                      myInput.close();
                  }
              }catch(IOException e){ }
              File dir1 = new File(externalStorage_path + "/yourfilename");
              dir1.mkdir();
              File mfile1 = new File(dir,filename);
              return mfile1;
        }
   }else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
       showToast("External storage has readonly access");
   } else if (Environment.MEDIA_REMOVED.equals(state)) {
       showToast("External storage not present");
   } else if (Environment.MEDIA_UNMOUNTABLE.equals(state)){
       showToast("External storage cannot be mounted. Sdcard problem");
   }

これにより、ファイルがストレージに書き込まれ、Adobe などの別のアプリケーションで共有して開くことができます。このメソッドを呼び出しました。

于 2012-09-05T12:43:46.877 に答える