Android Uri to Filesize [duplicate]
Asked Answered
C

1

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

Chunky answered 10/3, 2011 at 17:31 Comment(5)
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.Gracielagracile
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.Pelayo
@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.Junko
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.Gracielagracile
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.Rosenblatt
D
27

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

File f = new File(uri.getPath());
long size = f.length();
Dipsomania answered 10/3, 2011 at 17:37 Comment(4)
File expects java.net.URI, but I have andorid.net.UriChunky
This SO post shows how to convert between the two: #560402Ori
This always returns 0 for me, see my answer hereCerargyrite
Working well good solutionAllegraallegretto

© 2022 - 2024 — McMap. All rights reserved.