2

I am building an application that requires a local .pdf file to be viewed in. So i have made a android_asset with the required file included
Directory Structure: Invoice/app/src/main/android_asset/originalInvoice.pdf

But I keep getting an error when trying to open it:
WEBPAGE NOT AVAILABLE
The webpage at
file:///android_asset/originalInvoice.pdf
Could not be loaded because:
net::ERR_FILE_NOT_FOUND

Here is my 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);
}

And This Is My 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>

I am vaguely aware that kit-kat fixed some vulnerabilities in the WebView Application and that this may be causing my problems. I just dont know how to fix it
If this is any help i am developing on API 19 or higher.
I am quite new to android programming, so i am trying my best here :)
Thanks In Advance!
~Kevin Grout

Kevin G.
  • 43
  • 4

1 Answers1

1

WebView can't display PDFs by itself. There are at least basic approaches for viewing PDFs (see Render a PDF file using Java on Android):

  1. Display in WebView via Google Docs (this will require internet connection).

  2. Fire an intent to open PDF viewer installed on the system, or open Play Store to install one.

  3. Use PdfRenderer view class added in Lollipop.

Community
  • 1
  • 1
Mikhail Naganov
  • 6,643
  • 1
  • 26
  • 26