Enable Webview Downloads

Asked

Viewed 1,438 times

3

Hello I’m developing an app with WebView and I realized that it is not possible to download files from the web.

I tried to download a Dropbox file,

It is possible that before downloading the file an alert will appear showing the following:

Want to download: Nome-Do-Arquivo and the buttons Sim and Não

Code already made:

package br.and.browser;

import ...

public class MainActivity extends Activity {

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

        getActionBar().setDisplayShowHomeEnabled(false);
        getActionBar().setDisplayShowTitleEnabled(false);
        getActionBar().hide();

        final EditText url = (EditText) findViewById(R.id.edittext_url);
        final WebView myWebView = (WebView) findViewById(R.id.webview);

        ImageButton Back = (ImageButton) findViewById(R.id.button_back);
        ImageButton Next = (ImageButton) findViewById(R.id.button_next);
        ImageButton Reload = (ImageButton) findViewById(R.id.button_reload);

        myWebView.setWebViewClient(new WebViewClient());
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.getSettings().setSupportZoom(true);
        myWebView.getSettings().getBuiltInZoomControls();
        myWebView.invokeZoomPicker();
        myWebView.loadUrl("https://www.google.com/");


        Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myWebView.goBack();
            }
        });

        Next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myWebView.goForward();
            }
        });

        Reload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myWebView.reload();
            }
        });

        url.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                boolean handled = true;
                if (actionId == EditorInfo.IME_ACTION_GO) {
                    myWebView.loadUrl(url.getText().toString());
                    return true;
                }
                return true;
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        WebView myWebView = (WebView) findViewById(R.id.webview);
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }



    @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);
    }
}
  • 1

    It came to implement the setDownloadListener? I think this question might help you: http://stackoverflow.com/questions/10069050/download-file-inside-webview.

  • I already did that but when trying to download the file opens the default browser

1 answer

0

I got through the setDownloadListener, but in the version of android 6+ the app stops working!

Method to download PDF

wv.setDownloadListener(new DownloadListener() {

    public void onDownloadStart(String url, String userAgent,
                                String contentDisposition, String mimetype,
                                long contentLength) {
        DownloadManager.Request request = new DownloadManager.Request(
                Uri.parse(url));

        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "boleto.pdf");
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
        intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
        intent.setType("*/*");//any application,any extension
        Toast.makeText(getApplicationContext(), "Baixando Arquivo", //To notify the Client that the file is being downloaded
                Toast.LENGTH_LONG).show();

    }
});

Browser other questions tagged

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