2015-07-11 151 views
-1

我试图将文件从资源文件夹命名的子文件夹复制,但尝试使用该文件时遇到了“未找到错误”。显然它似乎没有复制该文件的权利。文件中的资源文件夹

这里是我做了什么,也许有人能发现我的错误

方法调用

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因为该文件似乎从来没有复制,但当我删除“棒棒糖”目录,它工作正常。什么似乎不对? TNX

回答

0

访问的资源文件夹子目录,因为解释这种那些有问题并没有明确回答,这是怎么实现它。

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/ 
0

openFileOutput()不接受子目录。由于of/lollipop/proxy.sh,你要创建一个子目录。

+0

,就可以得到任何更好的方法来复制该文件在正常吗? – CodeZero

+0

@CodeZero:这取决于你在本地文件系统上想要什么。如果你想在本地文件系统,你有'资产/'相同的目录结构,使用'getFilesDir()''而不是openFileOutput()'。如果你只是简单地拷贝这个文件并且不需要'lollipop /'作为本地文件系统的一个目录,那么调用''''''''''''''来获取文件名,然后传递'getName() ''openFileOutput()'的结果。 – CommonsWare

+0

花了2天时间,没有你的解决方案。 – CodeZero

相关问题