2013-10-24 38 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

不,确定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

OK destfile是ArrayList的你HV用列表的名称相反,你可以使用列表而不是目标文件变量 – KOTIOS

+0

对不起,你可以告诉我完整的方式吗?我在Android的知识是非常有限的,我有4个文件要复制,让我们说FileA,FileB,FileC和FileD,谢谢 – user2872856

相关问题