eclipse 将目录从资产复制到数据文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16983989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
copy directory from assets to data folder
提问by yanniks
I would like to copy a quite big directory from the assets folder of my app to the data folder on the first run of the app. How do I do that? I already tried some examples, but nothing worked, so I don't have anything. My target is Android 4.2.
我想在第一次运行应用程序时将一个相当大的目录从我的应用程序的资产文件夹复制到数据文件夹。我怎么做?我已经尝试了一些例子,但没有任何效果,所以我什么都没有。我的目标是 Android 4.2。
Thanks, Yannik
谢谢,亚尼克
回答by matreshkin
try this code of your Application instance (you should write the class in manifest): This code is copying content of assets/files folder to the cache folder of app (you can place other path in copyAssetFolder() function). Only when App is launched for the first time
试试你的应用程序实例的这段代码(你应该在清单中编写类):这段代码将 assets/files 文件夹的内容复制到 app 的缓存文件夹中(你可以在 copyAssetFolder() 函数中放置其他路径)。仅当第一次启动应用程序时
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Application;
import android.content.Context;
import android.content.res.AssetManager;
import android.preference.PreferenceManager;
public class MyApplication extends Application {
private static Context s_sharedContext;
@Override
public void onCreate () {
super.onCreate();
if (!PreferenceManager.getDefaultSharedPreferences(
getApplicationContext())
.getBoolean("installed", false)) {
PreferenceManager.getDefaultSharedPreferences(
getApplicationContext())
.edit().putBoolean("installed", true).commit();
copyAssetFolder(getAssets(), "files",
"/data/data/com.example.appname/files");
}
}
private 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);
}
}
}