这是我在网上找到的将资产中的一个文件复制到内部存储的原始代码:
Context Context = getApplicationContext();
String DestinationFile = Context.getFilesDir().getPath() + File.separator + "DB.sqlite";
if (!new File(DestinationFile).exists()) {
try {
CopyFromAssetsToStorage(Context, "Database/DB.sqlite", DestinationFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void CopyFromAssetsToStorage(Context Context, String SourceFile, String DestinationFile) throws IOException {
InputStream IS = Context.getAssets().open(SourceFile);
OutputStream OS = new FileOutputStream(DestinationFile);
CopyStream(IS, OS);
OS.flush();
OS.close();
IS.close();
}
private void CopyStream(InputStream Input, OutputStream Output) throws IOException {
byte[] buffer = new byte[5120];
int length = Input.read(buffer);
while (length > 0) {
Output.write(buffer, 0, length);
length = Input.read(buffer);
}
}
以上代码适用于复制一个文件。但是,我想要的是复制多个文件而不是一个文件。在MT8之后,我将代码修改为:
public class MainActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> destFiles = new ArrayList<String>();
destFiles.add("FileB.jpg");
destFiles.add("FileC.jpg");
destFiles.add("FileD.jpg");
for(int i =0 ; i < destFiles.size(); i++) {
Context Context = getApplicationContext();
String DestinationFile = Context.getFilesDir().getPath() + File.separator + "FileA.db";
if (!new File(DestinationFile).exists()) {
try {
CopyFromAssetsToStorage(Context, "database/FileA.db", destFiles.get(i));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private void CopyFromAssetsToStorage(Context Context, String SourceFile, String DestinationFile) throws IOException {
InputStream IS = Context.getAssets().open(SourceFile);
OutputStream OS = new FileOutputStream(DestinationFile);
CopyStream(IS, OS);
OS.flush();
OS.close();
IS.close();
}
private void CopyStream(InputStream Input, OutputStream Output) throws IOException {
byte[] buffer = new byte[5120];
int length = Input.read(buffer);
while (length > 0) {
Output.write(buffer, 0, length);
length = Input.read(buffer);
}
}
}
但是,不会复制文件。我做错了的任何部分?
答案 0 :(得分:1)
Step 1 : u need to put the All files name in Arraylist first say ArrayList<String> destFiles .
ArrayList<String> destFiles = new ArrayList<String>();
destFiles.add("FileA");
destFiles.add("FileB");
destFiles.add("FileC");
Step 2 : For loop :
for(int i=0;i<destFiles.size;i++)
{
Context Context = getApplicationContext();
String DestinationFile = Context.getFilesDir().getPath() + File.separator + "DB.sqlite";
if (!new File(DestinationFile).exists()) {
try {
CopyFromAssetsToStorage(Context, "Database/DB.sqlite", destFiles.get(i));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void CopyFromAssetsToStorage(Context Context, String SourceFile, String DestinationFile) throws IOException {
InputStream IS = Context.getAssets().open(SourceFile);
OutputStream OS = new FileOutputStream(DestinationFile);
CopyStream(IS, OS);
OS.flush();
OS.close();
IS.close();
}
private void CopyStream(InputStream Input, OutputStream Output) throws IOException {
byte[] buffer = new byte[5120];
int length = Input.read(buffer);
while (length > 0) {
Output.write(buffer, 0, length);
length = Input.read(buffer);
}
}
}