使用缓存文件生成android apk
我正在制作一个android webview应用程序,有很多文件,比如js/css/images必须从CDN下载到应用程序中,因为我们地区的网络不稳定,缓存文件的大小比应用程序本身大很多,是否有某种方法可以在应用程序最初几次运行时使用自动存储的缓存文件构建apk文件。将文件和文件夹放入资源中。您可以在项目目录中找到它。当应用程序运行时,将所有资产内容复制到SD卡。然后运行你的应用:)使用缓存文件生成android apk,android,cordova,webview,hybrid-mobile-app,Android,Cordova,Webview,Hybrid Mobile App,我正在制作一个android webview应用程序,有很多文件,比如js/css/images必须从CDN下载到应用程序中,因为我们地区的网络不稳定,缓存文件的大小比应用程序本身大很多,是否有某种方法可以在应用程序最初几次运行时使用自动存储的缓存文件构建apk文件。将文件和文件夹放入资源中。您可以在项目目录中找到它。当应用程序运行时,将所有资产内容复制到SD卡。然后运行你的应用:) 如果您需要有关如何将资产内容复制到SD卡的任何帮助,请告诉我。将资产内容复制到SD卡 下面的代码将您的资产的指定
如果您需要有关如何将资产内容复制到SD卡的任何帮助,请告诉我。将资产内容复制到SD卡 下面的代码将您的资产的指定文件夹的所有内容复制到SD卡的指定位置 CopyAssetContents.java 公共类CopyAssetContent{
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卡的根目录中。
调用bellow方法
CopyAssetContents.copyAssetFolder(getAssets(), "CONTENTS", Environment.getExternalStorageDirectory().getAbsolutePath());
对不起,我以前从未制作过应用程序。请告诉我更多的细节,如何做这件事,谢谢!我已经添加了另一个答案。请查看。