android - Manually Handle MP3 Download in WebView -
I have a snippet of code in which I want to handle the download of the file being clicked:
else if (url.startsWith ("http://rapahh.com/songs2/Music%20Promotion/Download/")) {} return false; Although I do not know how to handle downloads in Android, so anyone has a snippet code, I can use the file to download in a folder in the background Am .. download folder is ok Thank you.
What version of Android are you making?
The API Lvl 9 is there which can handle it for you. If possible, you should use DownloadManager because it will automatically handle network interception and resume downloads for you.
If you are aiming for less API lvl, then you have to make the download code yourself. You will have an input stream from your web source and an outputstream is going to your local file and you will not have to leave any one until the loop through the interstitial writing section. Something like this:
try {url = url = new URL (URL); // Video URL // Set our file to correct path and name. File file = new file (pat + filename); // Keep the start time so that we can demonstrate how long it will take to log in. Long startTime = System.currentTimeMillis (); Log D. (Matag, "downloading beginners"); // lag D. (MyType, "Download URL:" + url); Log D. (MyTag, "Downloaded file name:" + fileName); / * Open a connection to that URL * / URLConnection ucon = url.openConnection (); // This will be useful so that you can show the 0-100% progress bar int lenghtOfFile = ucon.getContentLength (); Logs. I (maritag, "opened connection"); / *************************************************************************************************** **************************************************************************************************** Define inputstream to read from URL connection **************************************************************** ******** / Incoming flow is = ucon.getInputStream (); BufferedInputStream bis = new BufferedInputStream (is); Logs. I (Maritag, "foundstream and buffer facts found"); / *************************************************************************************************** **************************************************************************************************** Define the outputstream to write in our file. **************************************************************************************************** ************************** FileOutputStream Fos = New FileOutputStream (file); BufferedOutputStream Boss = New BufferedOutputStream (fos); Logs. I (Maritimate, "FileAutputuate Stream and Buffered Optical Stream"); / *************************************************************************************************** **************************************************************************************************** Read and write our files. ************************************************************************** **************************************************************************************************** **** Byte data [] = New byte [1024]; Long total = 0; integer number; // Loop and read the current section (count = bis.read (data))! = -1) {// Post your progress update UI thread post back to progress (progress (integer) (total * 100 / lenffile) ); // Write this section total + count =; boss. Write (data, 0, count); } // Call flush or the video file can be corrupt and will not play correctly bos.flush (); Bos.close (); Log D. (MerryTag, "Download in Ready" + ((System Syrtetime Milliseau - Starttime) + "Milicec"); } Hold (IOException e) {log d. (MyTag, "Error:" + E); } You will need to implement post progression to inform the user what is appropriate for your code . Edit:
You can comment on logs to make it work. I'm leaving them, while I'm debugging to simplify the process. The log status such as Log.i (string tag, string text) is similar to System.out.println (string textile) The difference is that these statements are printed in these The log file (which you can see in the DDMS perspective, in the eclipse) and they have an extra parameter called "tag", which you can add to whatever string you want, and this string will be displayed with your text in the log file. You can also filter the logout output on these tags in the DDS perspective. This is a common thing to declare your tag as a static string so that you can use that context for all of your log statements and you always guarantee for the same tag. So if you add something like this to your class, then you should fix your error: the last fixed string myTag = "NameOfYourActivity";
Comments
Post a Comment