Reputation: 6561
I would like to get the filesize of a file located in the assets directory. I saw many examples like
InputStream myInput = appContext.getAssets().open()
I know i can determine filesize by simple reading InputStream
in cycle but I'm looking for a way to do this via File object
.
So i need a path to assets and thats the thing i can't determine... How to get that path?
Also tried getting AssetFileDescriptor
via appContext.getAssets().openFd()
and openNonAssetFd()
but no luck - got FileNotFound
or probably compressed exceptions.
BTW those openFd methods are "pretty described" at developer.android.com.
file = new File(new URI("file:///android_asset/db/call3.db"));
Log.i(APP_TAG, "File name: "+file.getName());
Log.i(APP_TAG, "File path: "+file.getPath());
Log.i(APP_TAG, "File length: "+file.length());
Log.i(APP_TAG, "File isFile: "+file.isFile());
Log.i(APP_TAG, "File exists: "+file.exists());
which outputs to log:
04-13 12:10:03.413: INFO(6259): File name: call3.db
04-13 12:10:03.432: INFO(6259): File path: /android_asset/db/call3.db
04-13 12:10:03.444: INFO(6259): File length: 0
04-13 12:10:03.444: INFO(6259): File isFile: false
04-13 12:10:03.444: INFO(6259): File exists: false
of course the size of file is not 0 it is 195 584 bytes.
Upvotes: 9
Views: 6592
Reputation: 407
I am not sure if there is any way to get a File object from an assets folder but to find out the length of a file in single line, you can use the following code.
AssetFileDescriptor afd = context.getAssets().openFd(fileName);
long size = afd.getLength();
Upvotes: 2
Reputation: 1006614
How to get that path?
There is no path. What you are seeking is not possible, sorry.
Upvotes: 5