gpt4 book ai didi

android - 从 assets 文件夹加载大于 1M 的文件

转载 作者:IT王子 更新时间:2023-10-29 00:10:36 24 4
gpt4 key购买 nike

我快疯了,我创建了一个文件对象,所以它可以用 ObjectInputStream 读取,我放置了 assets 文件夹。该方法适用于小于 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 on line "ObjectInputStream ois=new ObjectInputStream(is);"

为什么??

最佳答案

面临同样的问题。我已经将我的 4MB 文件切成 1MB 的 block ,并在第一次运行时将这些 block 加入到手机上的数据文件夹中。作为额外的奖励,APK 被正确压缩。 block 文件称为 1.db、2.db 等。代码如下:

File Path = Ctxt.getDir("Data", 0);
File DBFile = new File(Path, "database.db");

if(!DBFile.exists() || DatabaseNeedsUpgrade) //Need to copy...
CopyDatabase(Ctxt, DBFile);


static private void CopyDatabase(Context Ctxt, File DBFile) throws IOException
{
AssetManager assets = Ctxt.getAssets();
OutputStream outstream = new FileOutputStream(DBFile);
DBFile.createNewFile();
byte []b = new byte[1024];
int i, r;
String []assetfiles = assets.list("");
Arrays.sort(assetfiles);
for(i=1;i<10;i++) //I have definitely less than 10 files; you might have more
{
String partname = String.format("%d.db", i);
if(Arrays.binarySearch(assetfiles, partname) < 0) //No such file in assets - time to quit the loop
break;
InputStream instream = assets.open(partname);
while((r = instream.read(b)) != -1)
outstream.write(b, 0, r);
instream.close();
}
outstream.close();
}

关于android - 从 assets 文件夹加载大于 1M 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2860157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com