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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With