2011-12-30 73 views
5

我在我的資產目錄中有一個html文件,我必須使用Intent將其作爲瀏覽器應用程序加載。如何使用Android中的intent加載本地html頁面?

這裏是我的代碼,但它不工作:

startActivity (new Intent(Intent.ACTION_VIEW, 
       Uri.parse("file:///android_asset/Sample.htm"))); 

誰能幫助我?

+0

你也可以簡單的使用webview來顯示。 – 2011-12-30 06:28:21

回答

0

使用loadUrl()WebView方法如果faq.html常見存在於資產文件夾中的HTML文件,然後你可以使用

WebView html = (WebView) findViewById(R.id.webEulaView); 
html.loadUrl("file:///android_asset/faq.html"); 
+0

將被加載到WebView中。問題是如何從應用程序啓動瀏覽器。 – 2012-08-28 00:15:15

0

我有同樣的加載HTML頁面

樣本實施例 問題是我所做的是 將資產的內容複製到數據庫,然後從SD卡中將其拉下來

這裏是複製你的資產到SD卡 使用的邏輯如下的HTML頁面放置在資產文件夾中的zip文件 內容是在此之後的zip資產文件夾

boolean succussFlag = false; 
    destination=""; 
    destination=Environment.getExternalStorageDirectory()+"/"; 
    File file = new File(destination); 

    if (!file.exists()){ 
     file.mkdirs(); 
    } 
    else 
    { 
     //file.delete(); 
     //file.mkdir(); 
    } 
    try 
    { 
     InputStream fileInput = context.getAssets().open("content.zip"); 
     ZipInputStream inputStream = new ZipInputStream(fileInput); 

     for (ZipEntry entry = inputStream.getNextEntry(); entry != null; entry = inputStream.getNextEntry()) 
     { 
      String innerFileName = destination + entry.getName(); 
      System.out.println("destination::::"+innerFileName); 
      //    Log.v("inner file name 0",""+innerFileName); 
      File innerFile = new File(innerFileName); 
      if (innerFile.exists()) 
      { 

       innerFile.delete(); 
      } 

      // Check if it is a folder 
      if (entry.isDirectory()) 
      { 
       // Its a folder, create that folder 
       innerFile.mkdirs(); 
      } 
      else 
      { 
       //     System.out.println(" ::::::::::::::INNER FILE COPYING :::: " + innerFile.toString()); 
       FileOutputStream outputStream = new FileOutputStream(innerFileName); 
       final int BUFFER = 4096; 

       BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream, 
         BUFFER); 

       int count = 0; 
       byte[] data = new byte[BUFFER]; 
       while ((count = inputStream.read(data, 0, BUFFER)) != -1) 
       { 
        bufferedOutputStream.write(data, 0, count); 
       } 
       bufferedOutputStream.flush(); 
       bufferedOutputStream.close(); 
      } 

      inputStream.closeEntry(); 
     } 
     inputStream.close(); 
     //   System.out.println(" ::::::::::COPIED TO PRIVATE FOLDER :::: "); 
     succussFlag=true; 
    } 
    catch (IOException e) 
    { 
     //   System.out.println("** EXCEPTION OCCURED WHILE COPYING***"); 
     e.printStackTrace(); 
     succussFlag=false; 
    } 

    return succussFlag; 

的名稱代碼你給以下命令:

startActivity (new Intent(Intent.ACTION_VIEW,"file://"+  Environment.getExternalStorageDirectory()+"/content"+name_Html ; 
     ); 
+0

必須比從資產複製文件到SD卡更簡單的方法。這是沒有意義的。 – 2012-08-28 00:16:20

相關問題