简体   繁体   中英

Is there a way to open file as File object from android's assets folder?

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.


I'd tried next code:

 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.

How to get that path?

There is no path. What you are seeking is not possible, sorry.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM