32

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

Community
  • 1
  • 1
Soccertrash
  • 1,830
  • 3
  • 28
  • 48
  • 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. – Lasse V. Karlsen Mar 21 '12 at 09:44
  • 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. – Abandoned Cart Sep 02 '12 at 11:34
  • 4
    @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. –  Jul 25 '14 at 16:11
  • 1
    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. – Lasse V. Karlsen Jul 25 '14 at 16:16
  • See [here](https://developer.android.com/guide/topics/providers/document-provider.html), 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`. – Daniel Apr 13 '15 at 13:36

1 Answers1

27

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

File f = new File(uri.getPath());
long size = f.length();
Behr
  • 1,220
  • 18
  • 24
fredley
  • 32,953
  • 42
  • 145
  • 236