How do I run an executable in assets?

How to add executable to assets and run it in Android and show output?

I have an executable that will work. I guess there should be several in the code chmod

.

Thank.

+3


source to share


2 answers


Place the executable in a raw folder, then run it with ProcessBuilder or Runtime.exec as they are here. http://gimite.net/en/index.php?Run%20native%20executable%20in%20Android%20App



+2


source


here is my answer

put copyAssets () in your mainactivity.

user code:

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);
            File outFile = new File(getFilesDir(), filename);
            out = new FileOutputStream(outFile);
            copyFile(in, out);

        } catch(IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }
        finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // NOOP
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // NOOP
                }
            }
        }
    }
}
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);
    }
}

      

also here's the code to run the command

public String runcmd(String cmd){
    try {
        Process p = Runtime.getRuntime().exec(cmd);

BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
int read;
char[] buffer = new char[4096];
        StringBuffer out = new StringBuffer();
        while ((read = in.read(buffer)) > 0) {
            out.append(buffer, 0, read);
        }
        in.close();
        p.waitFor();

        return out.substring(0);

    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

      



you may need to change it to

String prog=  "programname";
String[] env= { "parameter 1","p2"};
File dir=  new File(getFilesDir().getAbsolutePath());
Process p = Runtime.getRuntime().exec(prog,env,dir);

      

to ensure correct parameter handling

also add this to your main code to check that the files are copied correctly

String s;
File file4 = new File(getFilesDir().getAbsolutePath()+"/executable");
file4.setExecutable(true);
s+=file4.getName();
s+=file4.exists();
s+=file4.canExecute();
s+=file4.length();
//output s however you want it

      

should write: filename, true, true, correct filelength.

+2


source







All Articles