Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run executable in assets?

Tags:

java

android

How can I add a executable into assets and run it in Android and show the output?

I've a executable that will work. I assume there will need to be some chmod in the code.

Thank you.

like image 732
user3110739 Avatar asked Sep 27 '22 09:09

user3110739


1 Answers

here is my answer

put copyAssets() to your mainactivity.

someone's 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 is code to run 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 proper parameter handling

also add this to your main code to check proper copying of files

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.

like image 124
user3110739 Avatar answered Oct 13 '22 03:10

user3110739