我想写一个项目,在首次运行或安装时,它会将资产文件夹(app_webview,缓存,数据库,文件,lib,shared_prefs)中的文件和文件夹复制到/data/data/com.example.app/
我看到的Button button1;
Intent intent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
CopyAssets();
}
private void CopyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("Files");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
for(String filename : files) {
System.out.println("File name => "+filename);
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("Files/"+filename); // if files resides inside the "Files" directory itself
out = new FileOutputStream(getFilesDir().getParent().toString() + "/" + filename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(Exception e) {
Log.e("tag", e.getMessage());
}
}
}
private 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);
}
像这样的代码在这里
我通过使用此代码解决了这个问题。您可以使用此代码将任何文件从Assets / files /发送到您想要的任何文件,方法是将getFilesDir()。getParent()替换为我将在下面提供的值,但我还没弄清楚如何发送文件夹
package com.paresh.copyfileassetstoAssets;
public class CopyFileAssetsToSDCardActivity1 extends { /** 在第一次创建活动时调用。 * /
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent = new Intent(CopyFileAssetsToSDCardActivity1.this, CopyFileAssetsToSDCardActivity2.class);
startActivity(intent);
}
});
button1 =(按钮)findViewById(R.id.button1);
{{1}}
}
}