I having a problem with this URL url its a 302 url and it redirects to this site when you run in brwoser http://mp3goo.io/ but when you post this on a download manager it shows 10 mb file and i downloaded and worked fine. And i checked with my android code it says -1 content length how to get the file size like download manager image
Asked
Active
Viewed 1.1k times
4
-
I think this answers the question better, as it supports larger file sizes than what Integer allows: https://stackoverflow.com/a/12271781/878126 – android developer May 12 '19 at 13:53
2 Answers
9
use this
new Thread(new Runnable() {
@Override
public void run() {
try {
URL myUrl = new URL(url);
URLConnection urlConnection = myUrl.openConnection();
urlConnection.connect();
int file_size = urlConnection.getContentLength();
Log.i("sasa", "file_size = " + file_size);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();

Maysam R
- 915
- 10
- 12
2
That will definitely work for you,
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("http://YOUR_URL_HERE");
int fileLength = url.openConnection().getContentLength();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

Dhruv Patel
- 1,529
- 1
- 18
- 27
-
1Content length is -1 is getting for this url it’s a very different url – user2037091 Nov 24 '17 at 13:59
-
-
This link http://songolum.com/file/ppdVkTxqhwcJu-CAzCgtNeICMi8mHZnKQBgnksb5o2Q/Ed%2BSheeran%2B-%2BPerfect.mp3?r=idz&dl=311&ref=ed-sheeran-perfect – user2037091 Nov 25 '17 at 06:58
-
How come `getContentLength` returns an `int` ? Shouldn't it be `long`? – android developer Feb 28 '19 at 08:38
-