如何将15Mb文件从assets文件夹复制到SD卡...?

时间:2011-02-08 05:23:50

标签: android

我有15Mb的数据库文件,我想在我的应用程序中使用。 我将该文件存储在assets文件夹中。 因为15Mb的大尺寸文件我不能将该文件复制到SD卡。 我尝试过所有的东西.. 使用输入流读取文件是否有任何限制。 我的代码适用于高达1Mb大小的数据文件,但它不支持大于3到4 Mb。 我将文件压缩,然后存储到资产文件夹。

这是我的代码:

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

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

也许你的内存不足了。尝试在每次迭代结束时删除entry对象。