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();
Author by
Soccertrash
Updated on October 06, 2020Comments
-
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 about 12 yearsI 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 over 11 yearsIt 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 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 almost 10 yearsI 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 about 9 yearsSee 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 about 13 yearsFile expects java.net.URI, but I have andorid.net.Uri
-
Kurtis Nusbaum over 12 yearsThis SO post shows how to convert between the two: stackoverflow.com/questions/559902/…
-
daka over 5 yearsThis always returns 0 for me, see my answer here
-
Daxesh Vekariya almost 4 yearsWorking well good solution