Get file size from url

user2037091 picture user2037091 · Nov 24, 2017 · Viewed 7.3k times · Source

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

Answer

Maysam R picture Maysam R · Dec 1, 2017

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();