blkerai
blkerai

Reputation: 341

How to get downloaded file size in android

i don't know how to get total file size. this gives me -1 always

double totalSize = 0;
URL url = new URL(Constants.AUDIOURL+kid+".mp3");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
totalSize = (double)urlConnection.getContentLength();

Upvotes: 0

Views: 245

Answers (2)

blkerai
blkerai

Reputation: 341

i solved by this code

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
urlConnection.setRequestProperty("Accept-Encoding", "identity");

Upvotes: 0

Tamilselvan S
Tamilselvan S

Reputation: 91

Please find below code snippet. It works for your situation.

try{
 URL uri = new URL(Constants.AUDIOURL+kid+".mp3");
 URLConnection urlconnection = uri.openConnection();
 urlconnection .connect();
 String fileLengthStr = urlconnection .getHeaderField("content-length");
 Log.d("File Size", fileLengthStr );     
}
catch(Exception exception){
 exception.printStackTrace();
}

Upvotes: 4

Related Questions