我正在从 SQL 数据库下载一张图片到 Android 设备上;一切正常,除了打开流需要很长时间(即使没有图片下载)。实际下载开始前大约需要 5 秒。这是我的代码 fragment :
URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
//input = connection.getInputStream();
InputStream input = new BufferedInputStream(url.openStream());
File file = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraApp" + "/testpic.jpg");
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
//---blabla progressbar update etc..
行 InputStream input = new BufferedInputStream(url.openStream());
给出了问题。关于如何加快速度的任何想法?
最佳答案
这是创建实际 TCP 连接的点。这是网络问题,不是编码问题。您无法在代码中执行任何操作来修复它。
关于java - URL.openStream 很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11005360/