【发布时间】:2015-06-26 17:46:11
【问题描述】:
我正在构建一个需要查看本地 .pdf 文件的应用程序。所以我制作了一个包含所需文件的 android_asset
目录结构:Invoice/app/src/main/android_asset/ originalInvoice.pdf
但是我在尝试打开它时不断收到错误消息:
网页不可用
网页位于
文件:///android_asset/originalInvoice.pdf
无法加载,因为:
net::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编程很陌生,所以我在这里尽力而为:)
提前致谢!
~凯文·格劳特
【问题讨论】: