在 WebView 中手动处理 MP3 下载

发布于 2024-12-03 05:52:49 字数 248 浏览 0 评论 0原文

我有这段代码,我想在其中处理被单击的文件的下载:

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


        }
        return false;

虽然我不知道如何在 Android 中处理下载,但有人有一段代码可以用来在后台下载文件吗到一个文件夹..下载文件夹就可以了。谢谢。

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 downloads 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深府石板幽径 2024-12-10 05:52:49

您正在为哪个版本的 Android 构建?

从 API lvl 9 开始,DownloadManager 可以为您处理此问题。如果可能的话,您应该使用 DownloadManager,因为它会自动处理网络中断并为您恢复下载。

如果您的目标是低于该级别的 API,则必须自己制作下载代码。您将有一个来自网络源的输入流和一个发送到本地文件的输出流,您将循环遍历输入流写入块,直到没有剩余。
像这样的事情:

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);
    }

您需要实现 postProgress(int Progress) 方法来执行适合您的应用程序的任何操作,以通知用户下载完成的百分比。

编辑:

您可以注释掉日志以使其正常工作。我在调试时将它们保持打开状态,以使过程更容易。记录语句,例如 Log.i(String tag, String text)
与 System.out.println(String txt) 类似,区别在于这些语句被打印到日志文件中(可以在 eclipse 的 DDMS 透视图中看到)并且它们有一个额外的参数,称为“tag”,您可以将任何您喜欢的字符串传递给它,该字符串将显示在日志文件中文本的旁边。您还可以在 DDMS 视角中过滤基于这些标签的日志输出。通常的做法是将标签声明为静态字符串,这样您就可以在所有日志语句中使用对该标签的引用,并且保证始终具有相同的标签。因此,如果您将这样的内容添加到您的类中,它应该可以修复您的错误:

final static String myTag = "NameOfYourActivity";

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文