4

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

user2037091
  • 57
  • 1
  • 2
  • 8
  • 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 Answers2

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