从资产复制多个文件到内部存储

从资产复制多个文件到内部存储

本文介绍了从资产复制多个文件到内部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是原来的code从资产拷贝一个文件到内部存储我在网上找到:

 上下文语境= getApplicationContext();
串DestinationFile = Context.getFilesDir()的getPath()+文件分割符+DB.sqlite。;
如果(!新文件(DestinationFile).exists()){
  尝试{
    CopyFromAssetsToStorage(上下文,数据库/ DB.sqlite,DestinationFile);
  }赶上(IOException异常五){
    // TODO自动生成catch块
    e.printStackTrace();
  }
}私人无效CopyFromAssetsToStorage(上下文的背景下,字符串的SourceFile,字符串DestinationFile)抛出IOException
  InputStream为= Context.getAssets()打开(的SourceFile)。
  OutputStream的OS =新的FileOutputStream(DestinationFile);
  CopyStream(IS,OS);
  OS.flush();
  OS.close();
  IS.close();
}
私人无效CopyStream(输入的InputStream,OutputStream的输出)抛出IOException
  字节[]缓冲区=新的字节[5120];
  INT长度= Input.read(缓冲);
  而(长度大于0){
    Output.write(缓冲液,0,长度);
    长度= Input.read(缓冲液);
  }
}

以上code是复制一个文件工作的罚款。不过,我要的是将多个文件拷贝,而不是一个文件。继MT8,我修改了code以下:

 公共类MainActivity延伸活动{    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);            ArrayList的<串GT; destFiles =新的ArrayList<串GT;();
            destFiles.add(FileB.jpg);
            destFiles.add(FileC.jpg);
            destFiles.add(FileD.jpg);            的for(int i = 0; I< destFiles.size();我++){
            上下文的背景下= getApplicationContext();
            串DestinationFile = Context.getFilesDir()的getPath()+文件分割符+FileA.db。;
            如果(!新文件(DestinationFile).exists()){
              尝试{
                CopyFromAssetsToStorage(上下文,数据库/ FileA.db,destFiles.get(ⅰ));
              }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
              }
            }
            }
    }            私人无效CopyFromAssetsToStorage(上下文的背景下,字符串的SourceFile,字符串DestinationFile)抛出IOException
              InputStream为= Context.getAssets()打开(的SourceFile)。
              OutputStream的OS =新的FileOutputStream(DestinationFile);
              CopyStream(IS,OS);
              OS.flush();
              OS.close();
              IS.close();
            }
            私人无效CopyStream(输入的InputStream,OutputStream的输出)抛出IOException
              字节[]缓冲区=新的字节[5120];
              INT长度= Input.read(缓冲);
              而(长度大于0){
                Output.write(缓冲液,0,长度);
                长度= Input.read(缓冲液);
              }
            }
}

不过,文件不会被复制。我确实错任何部分?


解决方案

 第1步:u需要把所有文件名的ArrayList首先要说的ArrayList<串GT; destFiles。
ArrayList的<串GT; destFiles =新的ArrayList<串GT;();
destFiles.add(FILEA);
destFiles.add(FILEB);
destFiles.add(FileC);第2步:For循环:的for(int i = 0; I< destFiles.size;我++)
{
上下文的背景下= getApplicationContext();
串DestinationFile = Context.getFilesDir()的getPath()+文件分割符+DB.sqlite。;
如果(!新文件(DestinationFile).exists()){
  尝试{
    CopyFromAssetsToStorage(上下文,数据库/ DB.sqlite,destFiles.get(ⅰ));
  }赶上(IOException异常五){
    // TODO自动生成catch块
    e.printStackTrace();
  }
}私人无效CopyFromAssetsToStorage(上下文的背景下,字符串的SourceFile,字符串DestinationFile)抛出IOException
  InputStream为= Context.getAssets()打开(的SourceFile)。
  OutputStream的OS =新的FileOutputStream(DestinationFile);
  CopyStream(IS,OS);
  OS.flush();
  OS.close();
  IS.close();
}
私人无效CopyStream(输入的InputStream,OutputStream的输出)抛出IOException
  字节[]缓冲区=新的字节[5120];
  INT长度= Input.read(缓冲);
  而(长度大于0){
    Output.write(缓冲液,0,长度);
    长度= Input.read(缓冲液);
  }
}
}

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?

解决方案
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);
  }
}
}

这篇关于从资产复制多个文件到内部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 17:50