如何将位于assets文件夹中的15Mb文件复制到SD卡中?

3

我有一个15Mb的数据库文件,想在我的应用程序中使用。 我将该文件存储在assets文件夹中。 由于这个15Mb的大文件,我无法将它复制到sdcard中。 我尝试了所有的方法... 读取文件时是否有任何限制? 我的代码可以很好地处理1Mb大小的数据文件,但不支持大于3到4Mb的文件。 我将我的文件压缩并存储到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);
    }
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接