3

I have this little code here when click a link on webview that is a link to a file, in this case .mp4. this code will go to default web browser and request app that can view this file type.

myWebView.setDownloadListener(new DownloadListener() {
    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);
    }
});

What i want is when i click that file link, it create a dialog that ask weather to download or view the file. If click download, i want to use DownloadManager class to handle it and download that file in the background and alert when completed. And if click view, i want to create intent that ask for app that can view this file without going to the web browser.

    private class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, final String url) {

        if (url.endsWith(".mp4")) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage(R.string.dialog_title)
                .setCancelable(false)
                .setPositiveButton(R.string.dialog_download, new DialogInterface.OnClickListener() {                        

                    public void onClick(DialogInterface dialog, int id) {
                                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                                request.setDescription("Some descrition");
                                request.setTitle("Some title");
                                // in order for this if to run, you must use the android 3.2 to compile your app
                                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.ext");

                                // get download service and enqueue file
                                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                                manager.enqueue(request);
                    }
                })
                .setNegativeButton(R.string.dialog_play, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int id) {
                        WebView myWebView = (WebView) findViewById(R.id.webview);                           
                        myWebView.setDownloadListener(new DownloadListener() {
                            public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype,
                                    long contentLength) {
                                Intent intent = new Intent();
                                intent.setAction(Intent.ACTION_VIEW);
                                intent.setDataAndType(Uri.parse(url), "video/mp4");
                                startActivity(intent);
                            }
                        });

                    }
                });
            AlertDialog alert = builder.create();
            alert.show();
        }
        return false;
    }
}

Now i got this code that prompt user to download or play mp4 file that the user clicked. But when i click Play or Download it is work until i click the link the second time, what's wrong with the code above if anyone can correct this please. thanks.

I'm very new to Android developing and java, if anyone could guide me through this it will help me learn faster. And also sorry for my English...

homi3kh
  • 241
  • 1
  • 6
  • 17

1 Answers1

0

The solution to your problem relies on intercepting the URL that your webview is trying to load. Create a WebViewClient and overwrite the shouldOverrideUrlLoading method:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.endsWith(".mp3")) { //or whatever other extension
      //Prompt user for action (save or view)
    }
    return false;
 }

Then, depending on what the user chooses, start an AsyncTask for downloading to handle downloading the file, or launch the intent for viewing.

Community
  • 1
  • 1
D Yao.
  • 391
  • 1
  • 6
  • `if (url.endsWith(".mp4")) { AlertDialog.Builder builder = new AlertDialog.Builder(?); builder.setMessage(R.string.dialog_title) .setCancelable(false) .setPositiveButton(R.string.dialog_download, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int ?) { // how i put async to download? } }) .setNegativeButton(R.string.dialog_play, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int ?) { // how to create intent to view mp4? } }); AlertDialog alert = builder.create(); return false; }` – homi3kh Aug 23 '12 at 08:01
  • follow the link i provided to use an asynctask for downloading. Creating the intent for viewing an mp4 can be accomplished similar like this: http://android-er.blogspot.ca/2011/05/launch-actionview-intent-to-play-mp4.html – D Yao. Aug 23 '12 at 14:25
  • i just update the question with the code to prompt user to download and play the file but there's some problem, when i click the link it not work until i click the link the second time, please help correct that code. – homi3kh Aug 23 '12 at 17:26