2017-04-05 41 views
0

點擊我有一個簡單的Android屏幕,該屏幕由一個按鈕,如下圖所示打開本地的HTML文件時,按鍵上的Android

Android screen with button

我也有一個本地webpage命名存儲在我的assets文件夾中的android上webpage.html工作室。

enter image description here

當Android的屏幕上button被點擊,我要開,我已經存儲在本地的網頁。如果我嘗試打開http://www.google.com,它工作得很好。但是現在打開任何本地頁面似乎都有問題。以下是我的MainActivity.java代碼片段 -

//click this button to open a web page 
 
    public void onOpenWebBrowser(View v) 
 
    { 
 
     Intent webPageIntent = new Intent(Intent.ACTION_VIEW); 
 
     webPageIntent.setData(Uri.parse("/Users/user/AndroidStudioProjects/ProjectName/app/src/main/assets/webpage.html")); 
 

 
     try { 
 
      startActivity(webPageIntent); 
 
     } catch (ActivityNotFoundException ex) { 
 
      // can do something about the exception. 
 
     } 
 
    } 
 

 
    @Override 
 
    public boolean onCreateOptionsMenu(Menu menu) { 
 
     // Inflate the menu; this adds items to the action bar if it is present. 
 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
 
     return true; 
 
    }

這裏是我的activity_main.xml文件的內容 -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    tools:context=".MainActivity" > 
 
    <Button 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_centerHorizontal="true" 
 
     android:layout_centerVertical="true" 
 
     android:onClick="onOpenWebBrowser" 
 
     android:text="Open Web Browser" /> 
 

 
</RelativeLayout>

PS - 該網頁能正常工作時,如果我從外面打開它,在一個br owser。請幫忙!

+0

你試圖打開一個默認的Web瀏覽本地頁面你的手機應用程序正確嗎?不在WebView上? – user1506104

+0

是的,我正在嘗試使用手機的默認網頁瀏覽器,但是我已經使用了一個webView,在我已經拉出了很多我的頭髮之後! @ user1506104 – TheLuminor

+0

可能的[從資產文件夾中打開HTML文件]的重複(http://stackoverflow.com/questions/29939174/open-html-file-from-assets-folder) – user1506104

回答

0

首先在你的activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      tools:context=".MainActivity" > 
      <Button 
       android:id="@+id/button" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:onClick="onOpenWebBrowser" 
       android:text="Open Web Browser" /> 

     <WebView 
      android:id="@+id/web_view" 
      android:layout_below="@+id/button" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 

     </RelativeLayout> 

現在添加的WebView佈局MainActivity.java

WebView webView = (WebView)findViewById(R.id.web_view); 
webView.loadUrl("file:///android_asset/filename.html"); 

這將工作!

希望得到這個幫助!

+0

它說'即使在我嘗試快速修復導入失敗後,也無法解析'webView'。 @Wilson Christian – TheLuminor

+0

我想我會在某處出錯,我得到一個錯誤,說網頁無法加載,因爲該文件沒有找到! :/ – TheLuminor

0

你可以試試這個。意向的地方: -

WebView wb = new WebView(this); 
    wb.loadUrl("file:///android_asset/index.htm"); 
    setContentView(wb); 

替換文件路徑這個虛擬URL路徑。

希望它可以幫助你。 :)

+0

我是否使用路徑'android_asset'?怎麼把我的文件夾命名爲剛剛'asset' @Rajshree蒂瓦里 – TheLuminor

+1

@TheLuminor使用您的文件夾名「資產」 –

+0

@TheLuminor是它爲你工作? –

0

您可以顯示一個HTML文本視圖,但只有當你只是在做基本的HTML標記,這將工作:

try 
{ 
    InputStream inputStream = getResources().getAssets().open("myFile.html"); 

    String html = IOUtils.toString(inputStream); 

    myTextView.setText(Html.fromHtml(html)); 
} 
catch (IOException exception) 
{ 
    myTextView.setText("Failed loading html."); 
}