【发布时间】:2013-02-01 11:04:37
【问题描述】:
我构建/复制了一个下载功能,该功能通过 URL 获取图像和视频并将它们下载到 Android 设备。
下载小图片时没问题。但是当试图获取超过2MB 的文件(通过WLAN!)时,它确实需要很长时间!一个 25MB 的视频大约需要 5 分钟,以此类推。
有什么想法可能会出错吗?
这是我的代码:
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
【问题讨论】:
-
也许有缓冲区大小的东西....使用下载管理器....最好的下载方式
-
谢谢,但你能提供一些代码或链接吗?
-
看下面有一个例子
-
我认为你应该增加读取缓冲区。 50 字节真的很差
-
最好是 2k, 4k:) (2048, 4096)。