Webview download file

Source: Internet
Author: User

First:


The android player supports "download and play". When you click "MP3 download link" in the system browser, it will automatically play the song and save it to the local device (I don't know if this is the case with a third-party browser, I think the system browser will automatically recognize the MP3 download link, and call the system player to play the video ).

Similar to this process, in the author's music player, when the user selects "Download song", it will be transferred to a webview, here, I direct the initial webview URL to "Custom". When I click the MP3 download link, the music will be downloaded to the root directory of sdcard. The code of the activity where webview is located is as follows:

Java code:

  1. Setcontentview (R. layout. Web );
  2. Web = (webview) findviewbyid (R. Id. Web );
  3. Web. setwebviewclient (New downloadwebviewclient (this ));
  4. Websettings S = web. getsettings ();
  5. S. setsaveformdata (false );
  6. S. setsavepassword (false );
  7. S. setusewideviewport (true );
  8. S. setjavascriptenabled (true );
  9. S. setlighttouchenabled (true );
  10. Web. setwebchromeclient (New webchromeclient (){
  11. Public void onprogresschanged (webview view, int progress ){
  12. // Activity and webview determine the progress of the progress bar based on the loading degree
  13. // The progress bar disappears automatically when it is loaded to 100%
  14. Context. setprogress (Progress * 100 );
  15. }
  16. });
  17. Web. loadurl ("http://www.top100.cn /");

Copy code

Web. setwebviewclient (New downloadwebviewclient (this); among them, downloadwebviewclient enables us to download the key MP3 file. It inherits from webviewclient. Here we override its public Boolean shouldoverrideurlloading (webview, string URL) method: In the method, we determine whether the clicked link is "Download MP3 files". If it is true, start a service to download MP3 files. The Code is as follows:

Java code:

  1. Public class downloadwebviewclient extends webviewclient {
  2. Private context;
  3. Public downloadwebviewclient (context ){
  4. This. Context = context;
  5. }
  6. @ Override
  7. Public Boolean shouldoverrideurlloading (webview view, string URL ){
  8. Log. I ("info", "open an URL ");
  9. String urlstr = "";
  10. // Store the decoded URL
  11. // For UTF-8 encoding
  12. If (isutf8url (URL )){
  13. Urlstr = utf8urldecode (URL );
  14. // If it is not UTF-8 encoded
  15. } Else {
  16. Urlstr = urldecoder. Decode (URL );
  17. }
  18. // If the link is to download the MP3 file in top100.cn
  19. If (URL. substring (URL. Length ()-4). Equals (". MP3") & URL. substring (221). Equals ")){
  20. Log. I ("info", "MP3 file ");
  21. String ss [] = urlstr. Split ("/");
  22. String musicname = ss [ss. Length-1]; // obtain the full name (including the suffix) of the music file)
  23. Log. I ("info", "musicfile:" + musicname );
  24. // Pass the download link and file name to the download Module
  25. Intent intent = new intent (context, downloadservice. Class );
  26. Intent. putextra ("url", URL );
  27. Intent. putextra ("musicname", musicname );
  28. Context. startservice (intent );
  29. }
  30. Return super. shouldoverrideurlloading (view, URL );
  31. }

Copy code

The URL Decoding Method is omitted here. Downloadservice is used to download an MP3 file and receive the URL and music file name transmitted by downloadwebviewclient. The Code is as follows:

Java code:

  1. Public class downloadservice extends Service implements runnable {// implement the runable Interface
  2. Private string url_str; // path of the online song
  3. Private file download_file; // The downloaded file
  4. Private int total_read = 0; // The length of the downloaded object (in bytes)
  5. Private int readlength = 0; // the length of one-time download (in bytes)
  6. Private int music_length = 0; // The length of the music file (in bytes)
  7. Private Boolean flag = false; // whether to stop downloading. True is used to stop downloading.
  8. Private thread downthread; // download thread
  9. Private string musicname; // The downloaded file name.
  10. @ Override
  11. Public ibinder onbind (intent ){
  12. Return NULL;
  13. }

Second:

When meitu appreciates the Android app, it involves the Android app download function. The app itself is also relatively simple, that is, it calls the corresponding web page through webview Control for display. At first, I thought it was a normal file download implementation. I only needed a link and then clicked it to download it. However, when I put it on my mobile phone and tried it, I didn't even find any response when I clicked the download link, click OK on the normal page, and click other common links. It turns out that webview does not enable the file download function by default. If you want to implement the file download function, you need to set the webview downloadlistener to download the file by implementing your own downloadlistener. The procedure is as follows:

1. Set downloadlistener for webview:

Webview. setdownloadlistener (New mywebviewdownloadlistener ());

2. Implement the mywebviewdownloadlistener class as follows:

[Java]
View plaincopy
  1. Private class mywebviewdownloadlistener implements downloadlistener {
  2. @ Override
  3. Public void ondownloadstart (string URL, string useragent, string contentdisposition, string mimetype,
  4. Long contentlength ){
  5. Uri uri = URI. parse (URL );
  6. Intent intent = new intent (intent. action_view, Uri );
  7. Startactivity (intent );
  8. }
  9. }

This is just to call the built-in browser in the system for download, and there is no webview file download, But it basically meets our application scenarios.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.