title: Getting size of an image(in kb or mb) selected from gallery programatically
body: <p>I am selecting an image from gallery.I want to determine the size of the image programatically in kb or mb.
  This is what I have written:</p>
  
  <pre><code>public String calculateFileSize(Uri filepath)
  {
      //String filepathstr=filepath.toString();
      File file = new File(filepath.getPath());
  
      // Get length of file in bytes
      long fileSizeInBytes = file.length();
      // Convert the bytes to Kilobytes (1 KB = 1024 Bytes)
      long fileSizeInKB = fileSizeInBytes / 1024;
      // Convert the KB to MegaBytes (1 MB = 1024 KBytes)
      long fileSizeInMB = fileSizeInKB / 1024;
  
      String calString=Long.toString(fileSizeInMB);
      return calString;
  }
  </code></pre>
  
  <p>The uri of the image when selected from gallery is coming perfectly.But the value of <code>fileSizeInBytes</code> is zero.I am calling this method on <code>onActivityResult</code> ,after selecting the image from gallery.I saw a few same questions asked here before.But none worked for me.Any solution?</p>