0

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();
Cœur
  • 37,241
  • 25
  • 195
  • 267
blkerai
  • 341
  • 1
  • 17

2 Answers2

4

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();
}
Tamilselvan S
  • 91
  • 1
  • 4
  • it gives this error java.lang.NullPointerException: println needs a message – blkerai Aug 22 '16 at 09:06
  • Am getting this File Size: 957134 for the URL: http://www.hdwallpapersfreedownload.com/uploads/large/cartoons/tom-and-jerry-best-friends-free-hd-wallpaper.jpg without any errror. Please provide your url. – Tamilselvan S Aug 22 '16 at 11:43
0

i solved by this code

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
urlConnection.setRequestProperty("Accept-Encoding", "identity");
blkerai
  • 341
  • 1
  • 17