Issue
I am trying to pass a path from Java's inbuilt file manager to ADB with java program on Linux to install apk on android device. When the code is executed the apk selected using file manager never gets installed.
Here is the code:
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"APK Files", "apk");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(getParent());
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You choose to open this file: " + chooser.getSelectedFile().getName());
File file = new File("");
System.out.println(file.getAbsolutePath().toString());
try {
Process p1 = Runtime.getRuntime().exec("adb kill-server"); //for killing old adb instance
Process p2 = Runtime.getRuntime().exec("adb start-server");
Process p3 = Runtime.getRuntime().exec("adb install \"" + file.getAbsolutePath() + "\"");
p3.waitFor();
Process p4 = Runtime.getRuntime().exec("adb kill-server");
} catch (Exception e1) {
System.err.println(e1);
}
The following code should install the apk:
Process p3 = Runtime.getRuntime().exec("adb install \"" + file.getAbsolutePath() + "\"");
Solution
I figured it out myself, and here is the code:
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("APK Files", "apk");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(getParent());
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
String filename = chooser.getSelectedFile().getName();
try {
String[] commands = new String[3];
commands[0] = "adb";
commands[1] = "install";
commands[2] = file.getAbsolutePath();
Process p1 = Runtime.getRuntime().exec(commands, null);
p1.waitFor();
} catch (Exception e1) {
System.err.println(e1);
}
}
Answered By - user2197593
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.