Android Uri to Filesize

23,206

You can get the size of the file in bytes thus:

File f = new File(uri.getPath());
long size = f.length();
Share:
23,206
Soccertrash
Author by

Soccertrash

Updated on October 06, 2020

Comments

  • Soccertrash
    Soccertrash over 3 years

    Possible Duplicate:
    Android: Get the size of a file in resources?

    I want to upload a file to a WebServer. To show the progress of the upload I need to know the complete size of the file. Since I have only the Uri of the file (got it from an Intent), I have no chance to get at the file's size. My workaround is to open an InputStream and count the bytes that are read on this InputStream. But this is circuitous. Is there an easier/conventional way to solve this issue?

    Thanks

    • Lasse V. Karlsen
      Lasse V. Karlsen about 12 years
      I opted to close this one as a duplicate instead of the other one, since the linked duplicate has an accepted answer and this here hasn't, even though time-wise this question was asked first.
    • Abandoned Cart
      Abandoned Cart over 11 years
      It would have been useful to include a link to the duplicate so future viewers are not forced to now search for a variation of this question when they end up here first.
    • Admin
      Admin almost 10 years
      @LasseV.Karlsen The linked question is specifically related to resources (as in R.drawables, etc), not just files from an Intent. It's pretty different.
    • Lasse V. Karlsen
      Lasse V. Karlsen almost 10 years
      I agree, it seems different, dunno why I closed it as a duplicate now. You should flag it for reopening, I'm no longer a moderator so I don't have special powers in these tags.
    • Daniel
      Daniel about 9 years
      See here, under the header Examine document metadata, for an example method showing how to get display name and size of a document from a given Uri.
  • Soccertrash
    Soccertrash about 13 years
    File expects java.net.URI, but I have andorid.net.Uri
  • Kurtis Nusbaum
    Kurtis Nusbaum over 12 years
    This SO post shows how to convert between the two: stackoverflow.com/questions/559902/…
  • daka
    daka over 5 years
    This always returns 0 for me, see my answer here
  • Daxesh Vekariya
    Daxesh Vekariya almost 4 years
    Working well good solution