开发者

Manually Handle MP3 Download in WebView

开发者 https://www.devze.com 2023-04-02 07:01 出处:网络
I have this snippet of code in which I want to handle the downlaod of the file being clicked: else if (url.startsWith(\"http://rapahh.com/songs2/Music%20Promotion/Download/\")) {

I have this snippet of code in which I want to handle the downlaod of the file being clicked:

else if (url.startsWith("http://rapahh.com/songs2/Music%20Promotion/Download/")) {


        }
        return false;

Although I have no idea how to handle downloa开发者_运维百科ds in Android, so does anyone have a snippet of code I can use to download the file in the background to a folder.. the download folder is fine. Thanks.


What version of android are you building for?

Starting with API lvl 9 there is the DownloadManager that can handle this for you. If at all possible you should use the DownloadManager, because it will automatically handle network interuptions and resume the downloads for you.

If you are aiming for lower API lvl than that you'll have to make the download code yourself. You'll have an inputStream coming from your web source and an outputStream going to your local file and you will loop through the inputStream writing chunks until there are none left. Something like this:

try {

        URL url = new URL(URL); //URL of the video

        //Set our file to the correct path and name. 
        File file = new File(PATH + fileName);

        //keep the start time so we can display how long it took to the Log.
        long startTime = System.currentTimeMillis();
        Log.d(myTag, "download begining");
        //Log.d(myTag, "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 a tipical 0-100% progress bar
        int lenghtOfFile = ucon.getContentLength();

        Log.i(myTag, "Opened Connection");

        /************************************************
         * Define InputStreams to read from the URLConnection.
         ************************************************/
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        Log.i(myTag, "Got InputStream and BufferedInputStream");

        /************************************************
         * Define OutputStreams to write to our file.
         ************************************************/
        FileOutputStream fos = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        Log.i(myTag, "Got FileOutputStream and BufferedOutputStream");

        /************************************************
         * Start reading the and writing our file.
         ************************************************/
        byte data[] = new byte[1024];
        long total = 0;
        int count;
        //loop and read the current chunk
        while ((count = bis.read(data)) != -1) {
            //Post our progress update back to the UI thread
            postProgress((int)(total*100/lenghtOfFile));

            //write this chunk
            total += count;
            bos.write(data, 0, count);
        }
        //Have to call flush or the video file can get corrupted and won't play correctly.
        bos.flush();
        bos.close();

        Log.d(myTag, "download ready in "
                + ((System.currentTimeMillis() - startTime))
                + " milisec");
    } catch (IOException e) {
        Log.d(myTag, "Error: " + e);
    }

You'll need to implement the postProgress(int progress) method to do whatever is appropriate for your application to inform the user of what percentage complete the download is.

Edit:

you can comment out the logs to get it to work. I leave them on while I am debugging though to make the process easier. Log statements such as Log.i(String tag, String text) are similar to System.out.println(String txt) The difference is that these statements are printed into the log file ( which you can see in the DDMS perspective in eclipse) And they have an additional parameter called "tag" you can pass it whatever string you like and this string will show up along side of your text in the log file. You can also filter the log output basted on these tags in the DDMS perspective. It is common practice to declare your tag as a static String so that you can just use that reference to it for all of your log statements and you are guaranteed to always have the same tag. So if you add something like this to your class it should fix your error:

final static String myTag = "NameOfYourActivity";
0

精彩评论

暂无评论...
验证码 换一张
取 消