【问题标题】:Copying file from subfolder in asset folder从资产文件夹中的子文件夹复制文件
【发布时间】:2015-07-11 11:25:08
【问题描述】:

我正在尝试从资产文件夹中的命名子文件夹复制文件,但在尝试使用该文件时出现“未找到错误”。显然它似乎没有正确复制文件。

这是我所做的,也许有人能发现我的错误

方法调用

copyfile("/lollipop/proxy.sh");

方法

    public void copyfile(String file) {
            String of = file;
         File f = new File(of);
            String basedir = getBaseContext().getFilesDir().getAbsolutePath();


        if (!f.exists()) {
                            try {
        InputStream in =getAssets().open(file);
        FileOutputStream out =getBaseContext().openFileOutput(of, MODE_PRIVATE);
       byte[] buf = new byte[1024];
        int len;
     while ((len = in.read(buf)) > 0) {
                 out.write(buf, 0, len);
               }
            out.close();
           in.close();

  Runtime.getRuntime().exec("chmod 700 " + basedir + "/" + of);
                    } catch (IOException e) {
                                Log.e(TAG, "Error reading I/0 stream", e);
                            }
                        }
                    }

尝试使用 proxy.sh 失败,因为该文件似乎从未被复制,但是当我删除“棒棒糖”目录时它工作正常。似乎有什么问题?天呐

【问题讨论】:

    标签: android fileoutputstream android-assets


    【解决方案1】:

    openFileOutput() 不接受子目录。由于of 指向/lollipop/proxy.sh,因此您正在尝试创建一个子目录。

    【讨论】:

    • 那么有没有更好的方法来正确复制文件?
    • @CodeZero:这取决于你在本地文件系统上想要什么。如果您希望本地文件系统上的目录结构与assets/ 中的目录结构相同,请使用getFilesDir() 而不是openFileOutput()。如果您只是想复制这个文件并且不需要将lollipop/ 作为本地文件系统上的目录,请在of 上调用getName() 以获取裸文件名,然后将该getName() 结果传递给@987654331 @.
    • 在这上面花了 2 天,但您的解决方案都不起作用。
    【解决方案2】:

    那些在访问资产文件夹中的子目录时遇到问题的人,因为对此的解释没有明确回答,这就是我实现它的方式。

    AssetManager assetManager = getAssets();
        String[] files = null;
        try {
          if (Build.VERSION.SDK_INT >= 21)
              files = assetManager.list("api-16");
          else
              files = assetManager.list("");
        } catch (IOException e) {
          Log.e(TAG, e.getMessage());
        }
        if (files != null) {
          for (String file : files) {
            InputStream in = null;
            OutputStream out = null;
            try {
    
              if (Build.VERSION.SDK_INT >= 21)
                in = assetManager.open("api-16/" + file);
              else
                in = assetManager.open(file);
              out = new FileOutputStream("/data/data/yourpackagename/" + file);
              copyFile(in, out);
              in.close();
              in = null;
              out.flush();
              out.close();
              out = null;
            } catch (Exception e) {
              Log.e(TAG, e.getMessage());
            }
          }
        }
      }
    
      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);
        }
      }
    

    方法调用 现在可以从

    访问文件
    /data/data/yourpackagename/  
    

    所以从那里调用文件。使用

    getFilesDir() 
    

    获取时将无法正常工作
    /data/data/yourpackagename/files/
    

    【讨论】:

      猜你喜欢
      • 2023-01-11
      • 2018-01-19
      • 2012-01-14
      • 2011-08-07
      • 2013-05-10
      • 2014-05-04
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多