Android Studio:WebView 问题
Android Studio: WebView Problems
我正在构建一个需要在其中查看本地 .pdf 文件的应用程序。所以我制作了一个 android_asset,其中包含所需的文件
目录结构: Invoice/app/src/main/android_asset/originalInvoice.pdf
但是我在尝试打开它时一直收到错误消息:
网页不可用
网页位于
文件:///android_asset/originalInvoice.pdf
无法加载,因为:
网络::ERR_FILE_NOT_FOUND
这是我的MainActivity.java
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/originalInvoice.pdf");
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
}
@Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
这是我的 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kgdev.invoice" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我隐约意识到 kit-kat 修复了 WebView 应用程序中的一些漏洞,这可能是我遇到问题的原因。我只是不知道如何解决它
如果这对我在 API 19 或更高版本上开发有任何帮助。
我对 android 编程还很陌生,所以我正在尽力而为 :)
提前致谢!
~凯文·格鲁特
WebView 本身无法显示 PDF。至少有查看 PDF 的基本方法(参见 Render a PDF file using Java on Android):
通过 Google Docs 在 WebView 中显示(这需要互联网连接)。
启动 Intent 以打开系统上安装的 PDF 查看器,或打开 Play 商店安装一个。
使用 Lollipop 中添加的 PdfRenderer 视图 class。
我正在构建一个需要在其中查看本地 .pdf 文件的应用程序。所以我制作了一个 android_asset,其中包含所需的文件
目录结构: Invoice/app/src/main/android_asset/originalInvoice.pdf
但是我在尝试打开它时一直收到错误消息:
网页不可用
网页位于
文件:///android_asset/originalInvoice.pdf
无法加载,因为:
网络::ERR_FILE_NOT_FOUND
这是我的MainActivity.java
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/originalInvoice.pdf");
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
}
@Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
这是我的 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kgdev.invoice" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我隐约意识到 kit-kat 修复了 WebView 应用程序中的一些漏洞,这可能是我遇到问题的原因。我只是不知道如何解决它
如果这对我在 API 19 或更高版本上开发有任何帮助。
我对 android 编程还很陌生,所以我正在尽力而为 :)
提前致谢!
~凯文·格鲁特
WebView 本身无法显示 PDF。至少有查看 PDF 的基本方法(参见 Render a PDF file using Java on Android):
通过 Google Docs 在 WebView 中显示(这需要互联网连接)。
启动 Intent 以打开系统上安装的 PDF 查看器,或打开 Play 商店安装一个。
使用 Lollipop 中添加的 PdfRenderer 视图 class。