0

This is the original code for copying one file from asset to internal storage I found online:

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);
  }
}

The above code is working fine for copying one file. However, what I want is to copy multiple files instead of one file. Following MT8, I modified my the code to below:

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);
              }
            }
}

However, the files won't be copied. Any part that I did wrongly?

user2872856
  • 1,955
  • 2
  • 19
  • 50

1 Answers1

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);
  }
}
}
KOTIOS
  • 11,234
  • 3
  • 37
  • 64
  • I put this above the code: `ArrayList list = new ArrayList(); list.add("FileA); list.add("FileB"); list.add("FileC");` But where should I put destFiles? – user2872856 Oct 24 '13 at 02:35
  • ok destfile is name of arraylist you hv used list instead you can use list instead of destfiles variable – KOTIOS Oct 24 '13 at 02:37
  • Sorry, can you show me the complete way? My knowledge in Android is really limited. I have 4 files to be copy, let say FileA, FileB, FileC and FileD. Thank you. – user2872856 Oct 24 '13 at 02:43
  • It's not working. Maybe I made some mistake. I have edited my question. Please take a look. Thank you. – user2872856 Oct 24 '13 at 03:20
  • I do not have enough reputation to chat. My complete source is as above, what mistake have I made? If I use back `CopyFromAssetsToStorage(Context, "database/FileA.db", DestinationFile);` instead of `CopyFromAssetsToStorage(Context, "database/FileA.db", destFiles.get(i));`, then FileA.db will be copied sucessfully. – user2872856 Oct 24 '13 at 03:26