什么是净编程Android中下载文件,最好的方法? [英] What is best way to download files from net programatically in android?

查看:140
本文介绍了什么是净编程Android中下载文件,最好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序下载的网页,他们围绕200MB的文件(拉链)文件的加载。因此,任何一个可以告诉我下载文件的最好方法。其实我的问题是关于code性能。请给我一个想法或一些code。如果可以下载。如何在两者之间处理错误和网络问题。

In my application downloading loads of files from web they were around 200Mb files(Zipped). So can any one tell me best way to download files. Actually my concern is about performance of code. Please gimme an idea or some code if possible to download . How to handle errors and network problem in between .

感谢你, SRINIVAS

Thanking you, Srinivas

推荐答案

下面是一些code,我最近写的只是为:

Here's some code that I recently wrote just for that:

    try {
      URL u = new URL("http://your.url/file.zip");
      InputStream is = u.openStream();

      DataInputStream dis = new DataInputStream(is);

      byte[] buffer = new byte[1024];
      int length;

      FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/" + "file.zip"));
      while ((length = dis.read(buffer))>0) {
        fos.write(buffer, 0, length);
      }

    } catch (MalformedURLException mue) {
      Log.e("SYNC getUpdate", "malformed url error", mue);
    } catch (IOException ioe) {
      Log.e("SYNC getUpdate", "io error", ioe);
    } catch (SecurityException se) {
      Log.e("SYNC getUpdate", "security error", se);
    }

该下载文件,并把它放在你的SD卡。

This downloads the file and puts it on your sdcard.

您也许可以修改这个来满足您的需求。 :)

You could probably modify this to suit your needs. :)

这篇关于什么是净编程Android中下载文件,最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆