我想获取资产目录中文件的文件大小。我看到很多例子像有没有办法从android资产文件夹中将文件作为File对象打开?
InputStream myInput = appContext.getAssets().open()
我知道我可以通过简单的阅读InputStream
在周期确定文件大小,但我在寻找一种方式通过File object
做到这一点。
所以我需要一个资产的路径,多数民众赞成我无法确定的事情......如何获得这条路径?
也尝试通过appContext.getAssets().openFd()
和openNonAssetFd()
AssetFileDescriptor
但没有运气 - 得到FileNotFound
或可能压缩的异常。
顺便说一下,这些openFd方法在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());
其输出登录:
12月4日至13日:10:03.413:INFO(6259):文件名:call3.db
12月4日至13日: 10:03.432:INFO(6259):文件路径:/android_asset/db/call3.db
04-13 12:10:03.444:INFO(6259):文件长度:0
04-13 12:10:03.444 :INFO(6259):文件isFile:false
04-13 12:10:03.444:INFO(6259):文件存在:false
当然文件大小不是0它是195 584字节。
好的。那么appContext.getAssets()。openFd()/ openNonAssetFd()呢?它是什么以及它为什么不起作用? – Stan 2011-04-14 06:09:50
有史以来最令人心碎的答案,但确实如此。 – 2012-10-22 09:38:45