Your error lies in this line:
url.openStream()
If we go to grepcode to the sources of this function, we will see:
public final InputStream openStream() throws java.io.IOException { return openConnection().getInputStream(); }
But you have already opened the connection, so open the connection twice.
As a solution, you need to replace url.openStream() with connection.getInputStream()
So your cut off will look like this:
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect(); int lenghtOfFile = connection.getContentLength(); Log.d("File Download", "Lenght of file: " + lenghtOfFile); InputStream input = new BufferedInputStream(connection.getInputStream());
source share