0

I am able to download a file from the server well using android download manager and save it in the sdcard.

Now i need to get the size of the file in the server to compare with the downloaded file on the sdcard. I am able to get the size of the downloaded file, now i need help on creating a method to return the size of the file in the server have tried this but is not working ..

  public  int getFileSizeAtURL(URL url)
        {


          int filesize = -1;

          try
          {
              URL urls = new URL(file_url);
              HttpURLConnection http = (HttpURLConnection)urls.openConnection();
              http.connect();
              filesize = http.getContentLength();
              http.disconnect();

          }
          catch(Exception e)
          {

          }

          return filesize;

        }

by using

           Toast.makeText(getApplicationContext(),getFileSizeAtURL(urls)+" kb", Toast.LENGTH_LONG).show(); 

It gives me the initialized value..-1kb how can i use this method so that it returns the filesize

                              Solved it !!!!

I solved my problem by placing this code on the main method

   if (android.os.Build.VERSION.SDK_INT > 9) {
                            StrictMode.ThreadPolicy policy = 
                                new StrictMode.ThreadPolicy.Builder().permitAll().build();
                            StrictMode.setThreadPolicy(policy);
                        } 
                      URL urls = new URL(file_url);
                      HttpURLConnection http = (HttpURLConnection)urls.openConnection();
                      http.connect();
                      int filesize = http.getContentLength();

                      http.disconnect();

It gives me the file size of the file in the server now i can compare with the file on the sdcard which is

    File file=Environment.getExternalStorageDirectory();
                        String filename = file.getPath() + "/test/testing";
                        File myfile = new File(filename);
                        long size = myfile.length()  

Enjoy guys !!!!

Simon Macha Mwas
  • 317
  • 2
  • 3
  • 9

1 Answers1

0
  if (android.os.Build.VERSION.SDK_INT > 9) {
                        StrictMode.ThreadPolicy policy = 
                            new StrictMode.ThreadPolicy.Builder().permitAll().build();
                        StrictMode.setThreadPolicy(policy);
                    } 
                  URL urls = new URL(file_url);
                  HttpURLConnection http = (HttpURLConnection)urls.openConnection();
                  http.connect();
                  int filesize = http.getContentLength();

                  http.disconnect();

It gives me the file size of the file in the server now i can compare with the file on the sdcard which is

   File file=Environment.getExternalStorageDirectory();
                    String filename = file.getPath() + "/test/testing";
                    File myfile = new File(filename);
                    long size = myfile.length() 
Simon Macha Mwas
  • 317
  • 2
  • 3
  • 9