在 WebView 中手动处理 MP3 下载
我有这段代码,我想在其中处理被单击的文件的下载:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在为哪个版本的 Android 构建?
从 API lvl 9 开始,DownloadManager 可以为您处理此问题。如果可能的话,您应该使用 DownloadManager,因为它会自动处理网络中断并为您恢复下载。
如果您的目标是低于该级别的 API,则必须自己制作下载代码。您将有一个来自网络源的输入流和一个发送到本地文件的输出流,您将循环遍历输入流写入块,直到没有剩余。
像这样的事情:
您需要实现
postProgress(int Progress)
方法来执行适合您的应用程序的任何操作,以通知用户下载完成的百分比。编辑:
您可以注释掉日志以使其正常工作。我在调试时将它们保持打开状态,以使过程更容易。记录语句,例如
Log.i(String tag, String text)
与 System.out.println(String txt) 类似,区别在于这些语句被打印到日志文件中(可以在 eclipse 的 DDMS 透视图中看到)并且它们有一个额外的参数,称为“tag”,您可以将任何您喜欢的字符串传递给它,该字符串将显示在日志文件中文本的旁边。您还可以在 DDMS 视角中过滤基于这些标签的日志输出。通常的做法是将标签声明为静态字符串,这样您就可以在所有日志语句中使用对该标签的引用,并且保证始终具有相同的标签。因此,如果您将这样的内容添加到您的类中,它应该可以修复您的错误:
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:
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: