Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Uri to Filesize [duplicate]

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

like image 387
Soccertrash Avatar asked Mar 10 '11 17:03

Soccertrash


People also ask

How to get the real file path from Android Uri?

2. How To Get The Real File Path From Android URI. At some time, only save the image URI in the android app is not enough, because the image URI may be changed, or the image URI is not changed but its related image may be removed. In this case, we need to save the URI-related images in the android app folder.

How do I load a file from a URI to imageviews?

If the URI is a Images.Media.EXTERNAL_CONTENT_URI (e.g. from Intent.ACTION_PICK for the Gallery) from you will need to look it up as in stackoverflow.com/q/6935497/42619 @Chlebta check out a library called Picasso for loading Files (even URLs) into ImageViews very easily.

Why can't I save the image Uri in the Android app?

At some time, only save the image URI in the android app is not enough, because the image URI may be changed, or the image URI is not changed but its related image may be removed.

How to access other app-created files from the Android storage system?

Because from android SDK version 19, to make Android apps secure, android does not allow one Android app to access other app-created files from the android storage system directly. If your Android app wants to access other app-created files, you should get the file from its ContentProvider with a content URI.


1 Answers

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

File f = new File(uri.getPath()); long size = f.length(); 
like image 131
fredley Avatar answered Sep 23 '22 18:09

fredley