Android的 - 复制资产,内部存储资产、Android

2023-09-12 22:57:18 作者:要走就滚别墨迹

美好的一天!

我刚开始开发的机器人。在我的应用程序,我需要在我的资产文件夹中复制项目的内部存储。

I have just started developing for android. In my app, I need to copy the items in my assets folder to the internal storage.

我已经搜索了很多所以包括这里面它复制到​​外部存储。 Android:如何在资产到SD卡?

I have searched a lot on SO including this which copies it to the external storage. Android: How to copy files in 'assets' to sdcard?

这就是我想要实现: 我有一个目录已经美元的内部存储为X> Y>以Z p $ psent。我需要与一个文件复制至Y另一个到Z

This is what I want to achieve: I have a directory already present in the internal storage as X>Y>Z. I need a file to be copied to Y and another to Z.

谁能帮我用code段?我真的没有任何想法如何去这件事。

Can anyone help me out with a code snippet? I really don't have any idea how to go on about this.

对不起,我的英语不好。

Sorry for my bad English.

多谢了。

推荐答案

使用

 String out= Environment.getExternalStorageDirectory().getAbsolutePath() + "/X/Y/Z/" ; 

        File outFile = new File(out, Filename);

编辑在您的文献之后。链接答案。

After Editing in your ref. Link Answer.

  private void copyAssets() {
    AssetManager assetManager = getAssets();
    String[] files = null;
try {
    files = assetManager.list("");
} catch (IOException e) {
    Log.e("tag", "Failed to get asset file list.", e);
  }
 for(String filename : files) {
    InputStream in = null;
    OutputStream out = null;
    try {
      in = assetManager.open(filename);

      String out= Environment.getExternalStorageDirectory().getAbsolutePath() + "/X/Y/Z/" ; 

        File outFile = new File(out, Filename);


      out = new FileOutputStream(outFile);
      copyFile(in, out);
      in.close();
      in = null;
      out.flush();
      out.close();
        out = null;
      } catch(IOException e) {
          Log.e("tag", "Failed to copy asset file: " + filename, e);
         }       
       }
     }
     private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
      int read;
     while((read = in.read(buffer)) != -1){
       out.write(buffer, 0, read);
     }
   }