从assets文件夹加载大于1M的文件
我快疯了,我创建了一个文件对象,这样就可以用 ObjectInputStream 读取它,并且我放置了资产文件夹。 该方法适用于小于 1M 的文件,对于较大的文件会出错。 我读到这是 Android 平台的限制,但我也知道可以“轻松”避免。 例如,下载过《雷霆迅雷》的玩家可以很容易地看到在他们的assets文件夹中有一个18.9M大的文件。 这是我从 ObjecInputStream 读取 1 个对象的代码,
File f = File.createTempFile("mytempfile", "dat");
FileOutputStream fos = new FileOutputStream(f);
InputStream is = mc.getAssets().open(path,3);
ObjectInputStream ois=new ObjectInputStream(is);
byte[] data = (byte[]) ois.readObject();
fos.write(data);
fos.flush();
fos.close();
ois.close();
is.close();
现在我有一个未压缩的文件,我可以使用它而不必担心错误“此文件无法作为文件描述符打开;它可能已压缩”
此函数适用于文件小于 1M,较大的文件返回 java.io.IOException 在线“ObjectInputStream ois=new ObjectInputStream(is);”
为什么??
I'm going crazy, I created a file object, so it can be read with ObjectInputStream, and I placed the assets folder.
The method works with a file smaller than 1M, and give error with larger files.
I read that is a limit of Android platform, but I also know that can be "easily" avoided.
Those who have downloaded the game Reging Thunder, for example, can easily see that in their assets folder is a file 18.9M large.
This is my code for read 1 object from a ObjecInputStream
File f = File.createTempFile("mytempfile", "dat");
FileOutputStream fos = new FileOutputStream(f);
InputStream is = mc.getAssets().open(path,3);
ObjectInputStream ois=new ObjectInputStream(is);
byte[] data = (byte[]) ois.readObject();
fos.write(data);
fos.flush();
fos.close();
ois.close();
is.close();
now I have an uncompressed file and I can use it without worrying about the error "This file can not be opened as a file descriptor; it is probably compressed"
This function works well with files smaller than 1M, with bigger files return an
java.io.IOException on line "ObjectInputStream ois=new ObjectInputStream(is);"
why??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
面临同样的问题。我已将 4MB 文件切成 1MB 块,并且在第一次运行时,我将这些块加入到手机上的数据文件夹中。作为额外的好处,APK 被正确压缩。这些块文件称为 1.db、2.db 等。代码如下:
Faced the same issue. I've cut up my 4MB file into 1 MB chunks, and on the first run I join the chunks into a data folder on the phone. As an added bonus, the APK is properly compressed. The chunk files are called 1.db, 2.db, etc. The code goes like this:
限制在于压缩资产。如果资产未压缩,系统可以对文件数据进行内存映射,并使用 Linux 虚拟内存分页系统酌情拉入或丢弃 4K 块。 (“zipalign”工具确保未压缩的资源在文件中字对齐,这意味着直接映射时它们也会在内存中对齐。)
如果资源被压缩,系统必须将整个资源解压缩到内存。如果您有 20MB 的资产,则意味着您的应用程序占用了 20MB 的物理内存。
理想情况下,系统将采用某种窗口压缩,以便只需要存在某些部分,但这需要资产 API 中的一些奇特功能以及能够与随机访问配合良好的压缩方案。现在 APK == Zip 使用“deflate”压缩,所以这是不切实际的。
您可以通过为资源提供未压缩的文件类型的后缀(例如“.png”或“.mp3”)来保持资源未压缩。您还可以在构建过程中使用“zip -0”手动添加它们,而不是通过 aapt 将它们捆绑起来。这可能会增加 APK 的大小。
The limitation is on compressed assets. If the asset is uncompressed, the system can memory-map the file data and use the Linux virtual memory paging system to pull in or discard 4K chunks as appropriate. (The "zipalign" tool ensures that uncompressed assets are word-aligned in the file, which means they'll also be aligned in memory when directly mapped.)
If the asset is compressed, the system has to uncompress the entire thing to memory. If you have a 20MB asset, that means 20MB of physical memory is tied up by your application.
Ideally the system would employ some sort of windowed compression, so that only parts need to be present, but that requires some fanciness in the asset API and a compression scheme that works well with random access. Right now APK == Zip with "deflate" compression, so that's not practical.
You can keep your assets uncompressed by giving them a suffix of a file type that doesn't get compressed (e.g. ".png" or ".mp3"). You may also be able to add them manually during the build process with "zip -0" instead of having them bundled up by aapt. This will likely increase the size of your APK.
就像塞瓦建议的那样,您可以将文件分成多个块。我用它来分割我的 4MB 文件。
如果您不需要再次将这些文件合并为设备上的一个文件,只需使用此 InputStream,它会即时将它们合并为一个文件。
用法:
ObjectInputStream ois = new ObjectInputStream(new SplitFileInputStream("mytempfile", ".dat", 4, getAssets()));
Like Seva suggested you can split up your file in chunks. I used this to split up my 4MB file
If you do not need to combine the files into a single file on the device again, just use this InputStream, which combines them into one on the fly.
Usage:
ObjectInputStream ois = new ObjectInputStream(new SplitFileInputStream("mytempfile", ".dat", 4, getAssets()));
将文件扩展名更改为 .mp3
Change the file extension to .mp3
我知道这是一个老问题,但我想到了一个很好的解决方案。
为什么不将已经预压缩的文件存储在 asset 文件夹中。
然后,由于它已经是一个 zip 文件,因此已压缩,因此不需要再次压缩。因此,如果您希望压缩文件以减小 apk 的大小,但又不想处理拆分文件的问题,我认为这更容易。
当您需要从设备读取该文件时,只需将输入流包装在 zipinputstream 中
http://developer.android.com/reference/java/util/zip /ZipInputStream.html
I know this is an old question but I thought of a good solution.
Why not store the file already pre-zipped in the asset folder.
Then since it is already a zip file and therefore compressed it won't need to be compressed again. So if you wanted the file to be compressed to decrease the size of your apk but you don't want to deal with splitting up files I think this is easier.
When you need to read that file off the device just wrap the inputstream in a zipinputstream
http://developer.android.com/reference/java/util/zip/ZipInputStream.html
我找到了另一种解决方案,也许你对此感兴趣。
在源的根目录中,您有
build.xml
文件,您可以覆盖custom_rules.xml
中的-package-resources
目标> 文件,用于在 ant 中添加/修改目标,而不会破坏标准 Android 应用程序构建系统中的任何内容。只需创建一个包含以下内容的文件:
I found another solution, maybe you're interested in it.
In the root of your sources, where you have the
build.xml
file, you can overwrite the-package-resources
target in thecustom_rules.xml
file, which is used for adding/modifying targets in ant without breaking anything in the standard android app build system.Just create a file with this content:
添加文件扩展名是 mp3。我在资产文件夹中使用 mydb.mp3 并复制。此运行没有错误。显示检查它。
add file extension is mp3.I use mydb.mp3in assets folder and copy .this run without error.show check it.
使用GZIP将是另一种方法。您只需将 InputStream 包装在 内GZIPInputStream。
我将其用于大小约为 3.0 MB 的数据库,输出压缩文件约为 600KB。
工具。
(这些过程是在编译APK文件之前完成的)。
并复制它:
Using GZIP would be another method. you only need to wrap InputStream inside GZIPInputStream.
I used this for a database which size about 3.0 MB and output compress file was about 600KB.
tool.
(these processes are done before compile APK FILE).
and copying it:
使用程序将数据库设置为多个部分,例如“Win Hex”,您可以从 链接
并继续从资源文件夹加载大于1M的文件
set database to multiple part with a program e.g "Win Hex", you can download from Link
and continue Load files bigger than 1M from assets folder
我将大文件放在原始文件夹中,而不是资产文件夹中。这对我有用。
Instead of assets folder, I have put my large files in the raw folder. It works for me.
我使用 NetBeans 构建包,但没有找到如何更改 AAPT 的设置。
我没有尝试过 png,但 mp3 是压缩的。
我可以编译包然后进入assets文件夹,参数-0?
正确的命令是什么?
I use NetBeans to build the package and I not found how to change the settings of AAPT.
I have not tried the png, but the mp3 are compressed.
I can compile the package and then enter the assets folder with the parameter -0?
what would be the correct command to use?