用缓存文件生成android apk

时间:2014-04-19 16:08:48

标签: android cordova webview hybrid-mobile-app

我正在制作一个android webview应用程序,有很多文件如js / css / images必须从CDN下载到应用程序,因为我们地区的网络不稳定,缓存文件的大小是比应用程序本身大得多,是否有一些方法来构建apk文件,当应用程序最初运行几次时自动存储缓存文件。

2 个答案:

答案 0 :(得分:0)

将您的文件和文件夹放入资源。你会在你的项目目录中找到它。当您的应用程序运行时,将所有资产内容复制到SD卡。然后运行你的应用程序:)

如果您需要有关如何将资产内容复制到SD卡的任何帮助,请通知我。

答案 1 :(得分:0)

将资产内容复制到SD卡

Bellow代码会将资源指定文件夹的所有内容复制到SD卡的指定位置

CopyAssetContents.java     公共类CopyAssetContents {

    public static boolean copyAssetFolder(AssetManager assetManager,String fromAssetPath, String toPath) {
        try {
            String[] files = assetManager.list(fromAssetPath);
            new File(toPath).mkdirs();
            boolean res = true;
            for (String file : files)
                if (file.contains("."))
                    res &= copyAsset(assetManager, 
                            fromAssetPath + "/" + file,
                            toPath + "/" + file);
                else 
                    res &= copyAssetFolder(assetManager, 
                            fromAssetPath + "/" + file,
                            toPath + "/" + file);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    private static boolean copyAsset(AssetManager assetManager,
            String fromAssetPath, String toPath) {
        InputStream in = null;
        OutputStream out = null;
        try {
          in = assetManager.open(fromAssetPath);
          new File(toPath).createNewFile();
          out = new FileOutputStream(toPath);
          copyFile(in, out);
          in.close();
          in = null;
          out.flush();
          out.close();
          out = null;
          return true;
        } catch(Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    private static void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
          out.write(buffer, 0, read);
        }
    }
}

例如,您将所有内容放在名为" CONTENTS"的文件夹中。在您的资产内部,并希望将其所有内容复制到SD卡的根目录。 叫贝罗法。

CopyAssetContents.copyAssetFolder(getAssets(), "CONTENTS", Environment.getExternalStorageDirectory().getAbsolutePath());
相关问题