How to enable PDF dowload on webView with Android Studio

Asked

Viewed 397 times

0

 //MainActivity

package app.inova.appfatto;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String url="http://seu site";
        WebView view=(WebView) this.findViewById(R.id.webView);
        view.loadUrl(url);
        view.setWebChromeClient(new WebChromeClient());
        view.getSettings().setJavaScriptEnabled(true);
        view.setWebViewClient(new WebViewClient());
    }
}
//AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app.inova.appfatto">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2 answers

0

Manifest

    <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Try with that stretch

 wv = (WebView) findViewById(R.id.webViewResultados);

        WebSettings ws = wv.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setSupportZoom(false);
        ws.setAllowFileAccess(true);

        wv.loadUrl("http://seu site");
        wv.setWebViewClient(new WebViewClient());

0

    // Adicionei este fragmento na minha MainAtivity

     view.setDownloadListener(new DownloadListener() {
                @Override
                public void onDownloadStart(String url, String userAgent,
                                            String contentDisposition, String mimetype,
                                            long contentLength) {
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                }
            });
       <!--E adicionei essas duas permissões para download na minha AndroidManifest -->
       
       <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.