2013-10-24 3 views
0

Это исходный код для копирования одного файла из актива внутренней памяти я нашел в Интернете:Копирование нескольких файлов из актива внутренней памяти

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

У вас есть дополнительные разрешения? – KOTIOS

+0

Имеется разрешение WRITE_EXTERNAL_STORAGE. Но я хочу скопировать файлы во внутреннее хранилище. Требуется ли другое копирование файла во внутреннее хранилище? – user2872856

+0

no, ok есть ли ошибка в logcat? – KOTIOS

ответ

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

Я положил это над кодом: 'ArrayList list = new ArrayList (); list.add ("FiLea); list.add (" FILEB "); list.add (" FileC ");?' Но где я должен поставить destFiles – user2872856

+0

нормально DestFile это имя Список_массивов вы Нч используется список вместо этого вы можете использовать список вместо переменной destfiles – KOTIOS

+0

Извините, вы можете показать мне полный способ? Мои знания в Android действительно ограничены. У меня есть 4 файла для копирования, скажем FileA, FileB, FileC и FileD. Спасибо. – user2872856

Смежные вопросы