How to calculate the size of a file in Android
I am looking to find the size of a file in Android. I originally expected it to be as simple as a size() method in File, or something along those lines. Bu开发者_如何学Got I haven't been able to find anything. Any help would be greatly appreciated.
Thanks
Use file.length()
http://download.oracle.com/javase/6/docs/api/java/io/File.html#length%28%29
The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist. Some operating systems may return 0L for pathnames denoting system-dependent entities such as devices or pipes.
File.length()
. You were close.
This is another solution.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (REQUEST_GET_IMAGE == requestCode && resultCode == Activity.RESULT_OK && data != null) {
Uri uri = data.getData();
try {
ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
Log.i("file size", String.valueOf(parcelFileDescriptor.getStatSize()));
} catch (Exception e) {
//escape logic here
}
}
}
精彩评论