Android的 - 复制资产,内部存储 [英] Android - Copy assets to internal storage

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

问题描述

美好的一天!

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

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

我已经搜索了很多所以包括这里面它复制到​​外部存储。 <一href="http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard">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);
     }
   }

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆