Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of file which we get through AssetManager function getAssets in Android

Tags:

java

android

AssetManager mngr = getAssets();
test_file = mngr.open("sample.txt");

above test_file variable is of InputStream type. Any way to calculate the file size of sample.txt from it?

like image 897
Dominic Avatar asked Aug 09 '11 06:08

Dominic


People also ask

How do I find file size on android?

file. length() returns the length of the file in bytes, as described in the Java 7 Documentation: Returns the length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist.

How do I read an asset file?

If you cannot open your ASSETS file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a ASSETS file directly in the browser: Just drag the file onto this browser window and drop it.


2 Answers

I have an alternative to get size of a file in assets using AssetFileDescriptor:

AssetFileDescriptor fd = getAssets().openFd("test.png");
Long size = fd.getLength();

Hope it helps.

like image 178
ayublin Avatar answered Nov 16 '22 01:11

ayublin


test_file.available();

Is not a very reliable method to get the file length as is stated in the docs.

size = fd.getLength();

Using the FileDescriptor as shown by Ayublin is!

His answer should be promoted to the correct answer.

like image 21
Michel Avatar answered Nov 16 '22 03:11

Michel