我有以下代码片段,我想在其中处理被单击文件的不足:

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


        }
        return false;


尽管我不知道如何在Android中处理下载,但是任何人都没有一小段代码片段,我可以使用该代码片段在后台将文件下载到文件夹中。.download文件夹很好。谢谢。

最佳答案

您要构建什么版本的android?

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

如果您要降低API lvl的水平,则必须自己制作下载代码。您将有一个来自Web源的inputStream和一个进入本地文件的outputStream,您将循环遍历inputStream编写块,直到没有块为止。
像这样:

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)相似,不同之处在于这些语句被打印到日志文件中(可以在DDMS透视图中的eclipse中看到),并且它们还有一个名为“ tag”的附加参数,您可以根据需要将其传递给任何字符串,并且字符串将显示在日志文件中文本的旁边。您还可以在DDMS透视图中过滤基于这些标记的日志输出。通常的做法是将标签声明为静态字符串,这样您就可以在所有日志语句中使用对该标签的引用,并且确保始终具有相同的标签。因此,如果您将类似这样的内容添加到您的课程中,则应该可以修复错误:

final static String myTag = "NameOfYourActivity";

10-08 08:45
查看更多