【问题标题】:Generate android apk with cache file使用缓存文件生成android apk
【发布时间】:2014-04-19 16:08:48
【问题描述】:

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

【问题讨论】:

    标签: android cordova webview hybrid-mobile-app


    【解决方案1】:

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

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

    【讨论】:

    • 对不起,我从来没有做过一个应用程序。你能告诉我更多细节如何做这件事,谢谢!
    • 我添加了另一个答案。看看吧。
    【解决方案2】:

    将资产内容复制到 SD 卡

    下面的代码会将您资产的指定文件夹的所有内容复制到您的 SD 卡的指定位置

    复制资产内容.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());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      相关资源
      最近更新 更多