Android Uri to Filesize

Soccertrash picture Soccertrash · Mar 10, 2011 · Viewed 21k times · Source

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

Answer

fredley picture fredley · Mar 10, 2011

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

File f = new File(uri.getPath());
long size = f.length();