URL.openStream muy lento

I'm downloading a picture on a android device from a SQL database; Everything works well, except that opening the Stream takes very long time (even if there is no picture to download). It takes approx 5 sec's before the actual download starts. Here is my code snippet:

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..

La línea InputStream input = new BufferedInputStream(url.openStream()); gives the problem. Any idea's on how to speed things up?

preguntado el 12 de junio de 12 a las 22:06

Are you SURE that the delay happens in that statement? I'd have expected it to occur in the connect() llamada. -

3 Respuestas

That's the point at which the actual TCP connection is created. It's a network problem, not a coding problem. Nothing you can do in the code to fix it.

Respondido el 12 de junio de 12 a las 23:06

Actually, the TCP connection is made a statement or two earlier ... because he'd getting the content length. But you are probably right that this is just network delay, and there's no fix for this in the Java client code. - Stephen C

@StephenC Yep. In that case it must be the getContentLength() that is taking the time. - user207421

I use this code to get a bitmap from a url. :)

Bitmap bitmap = null;
    URL imageUrl = new URL(url);
    HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
    conn.setConnectTimeout(30000);
    conn.setReadTimeout(30000);
    InputStream is = conn.getInputStream();
    OutputStream os = new FileOutputStream(f);

try
{
   byte[] bytes=new byte[1024];
   for(;;)
   {
      int count=is.read(bytes, 0, 1024);
      if(count==-1)
         break;
      os.write(bytes, 0, count);
    }
 }
 catch(Exception ex){}
os.close();
bitmap = decodeFile(f); 

Respondido el 12 de junio de 12 a las 23:06

Well it actually does, it about 2-3 seconds faster.. thanks! Maybe the use of HTTPURLConnection speeds things up.. - user1337210

@user1337210 No. You are already using it. The only difference here is the typecast. There is nohing here that answers the question. - user207421

Estas llamando url.openStream() al crear el InputStream, but prior to that you're creating a new connection and calling connection.connect().

From the android JavaDoc: openStream() is "Equivalent to openConnection().getInputStream(types)"

http://developer.android.com/reference/java/net/URL.html#openStream()

In summary I think you should call connection.getInputStream() when initialising the InputStream.

Respondido 02 Oct 12, 12:10

Both events happen anyway. The connect is implicit if you don't do it explicitly. See the Javadoc. - user207421

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.