0

Am trying to copy a file from a named subfolder in asset folder but am getting a "not found error" when trying to use the file. Apparently it seems am not copying the file right.

Here is what I have done maybe someone can spot my error

Method call:

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

Method:

    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);
                            }
                        }
                    }

Trying to use the proxy.sh fails as the file seems it's never copied but when I remove the " lollipop " directory it works fine. What seems wrong? Tnx

CodeZero
  • 179
  • 2
  • 15

2 Answers2

0

openFileOutput() does not accept subdirectories. Since of points to /lollipop/proxy.sh, you are trying to create a subdirectory.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So any better approach to copy the file in properly? – CodeZero Jul 11 '15 at 12:02
  • @CodeZero: That depends on what you want on the local filesystem. If you want the same directory structure on the local filesystem that you have in `assets/`, use `getFilesDir()` instead of `openFileOutput()`. If you are simply trying to copy this one file in and do not need `lollipop/` as a directory on the local filesystem, call `getName()` on `of` to get the bare filename, then pass that `getName()` result to `openFileOutput()`. – CommonsWare Jul 11 '15 at 12:06
  • Spent 2days on this and none of your solution works. – CodeZero Jul 13 '15 at 20:50
0

Those having issues accessing sub directories in asset folder since explanation to this isn't explicitly answered this is how I achieved it.

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);
    }
  }

method call Files are now accessible from

/data/data/yourpackagename/  

so call the files from there. Using

getFilesDir() 

won't work as it gets from

/data/data/yourpackagename/files/
CodeZero
  • 179
  • 2
  • 15