【发布时间】:2011-06-23 05:26:51
【问题描述】:
我有 15Mb 的数据库文件,我想在我的应用程序中使用。 我将该文件存储在资产文件夹中。 由于那个 15Mb 的大文件,我无法将该文件复制到 sdcard。 我已经尝试了所有的东西.. 使用输入流读取文件是否有任何限制。 我的代码适用于高达 1Mb 大小的数据文件,但它不支持大于 3to4 Mb 的数据文件。 我将我的文件压缩,然后存储到 assets 文件夹中。
这是我的代码:
private Thread thread = new Thread()
{
@Override
public void run()
{
// Create a directory in the SDCard to store the files
File file = new File(ROOT_FOLDER);
if (!file.exists())
{
file.mkdirs();
}
else
{
file.delete();
}
try
{
// Open the ZipInputStream
ZipInputStream in = new ZipInputStream(getAssets().open("lds_scriptures.zip"));
// Loop through all the files and folders
for (ZipEntry entry = in.getNextEntry(); entry != null; entry = in
.getNextEntry())
{
sendMessage("Extracting: " + entry.getName() + "...");
String innerFileName = ROOT_FOLDER + File.separator + entry.getName();
File innerFile = new File(innerFileName);
if (innerFile.exists())
{
innerFile.delete();
}
int size=in.available();
//Toast.makeText(SdCardData.this, String.valueOf(size),2000).show();
Log.e("value",String.valueOf(size));
// Check if it is a folder
if (entry.isDirectory())
{
// Its a folder, create that folder
innerFile.mkdirs();
}
else
{
// Create a file output stream
FileOutputStream outputStream = new FileOutputStream(innerFileName);
final int BUFFER = 2048;
// Buffer the ouput to the file
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream,
BUFFER);
// Write the contents
int count = 0;
byte[] data = new byte[BUFFER];
while ((count = in.read(data, 0, BUFFER)) != -1)
{
bufferedOutputStream.write(data, 0, count);
}
// Flush and close the buffers
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
// sendMessage("DONE");
// Close the current entry
in.closeEntry();
}
in.close();
// sendMessage("-----------------------------------------------");
// sendMessage("Unzipping complete");
}
catch (IOException e)
{
sendMessage("Exception occured: " + e.getMessage());
e.printStackTrace();
}
}
};
private Handler handler = new Handler()
{
// @Override
public void handleMessage(Message msg)
{
// tv.append("\n" + msg.getData().getString("data"));
// super.handleMessage(msg);
}
};
private void sendMessage(String text)
{
Message message = new Message();
Bundle data = new Bundle();
data.putString("data", text);
message.setData(data);
handler.sendMessage(message);
}
【问题讨论】:
标签: android