安卓。写入失败 : EPIPE (Broken pipe) when use Process

标签 android file epipe

您好,我尝试通过进程在我的应用程序中安装应用程序。对于该功能,我创建了这个方法。

private void loadAndInstallApk(String string) {
    if(!isRooted()){
        return;
    }
    Uri uri = loadApk(string);
    if(uri == null){
        return;
    }
    Process p = null;
    DataOutputStream outs = null;
    try
    {

        p = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});
        outs=new DataOutputStream(p.getOutputStream());

        String cmd="pm install -r " + uri.getPath();
        Log.d(TAG, "DATA = cmd = " + cmd);
        outs.writeBytes(cmd + "\n");

        // Close the terminal
        outs.writeBytes("exit\n");
        outs.flush();
            p.waitFor();
            if (p.exitValue() != 255) {
                Log.d(TAG, "DATA succerss " + p.exitValue());
                // Sucess 
            }
            else {
                Log.d(TAG, "DATA Fail " + p.exitValue());
                // Fail
            }
        }
    catch (IOException e){
        Log.e("Error", e.toString(), e);
    } catch (InterruptedException e) {
        Log.e("Error", e.toString(), e);
    } finally {
        if(outs != null) {
            try {
                outs.close();
            } catch (IOException e) {
                Log.d(TAG, e.toString(), e);
            }
        }
        try {
            File f = new File(uri.getPath());
            f.delete();
        }catch (NullPointerException e) {

            Log.e(TAG, "DATA = finally delete " + e.toString(), e);
        }
    }
}

但是调用失败了

        outs.writeBytes(cmd + "\n");

错误信息:

E/Error﹕ java.io.IOException: write failed: EPIPE (Broken pipe)
java.io.IOException: write failed: EPIPE (Broken pipe)
        at libcore.io.IoBridge.write(IoBridge.java:455)
        at java.io.FileOutputStream.write(FileOutputStream.java:187)
        at java.io.OutputStream.write(OutputStream.java:82)
        at java.io.DataOutputStream.writeBytes(DataOutputStream.java:156)
...
Caused by: libcore.io.ErrnoException: write failed: EPIPE (Broken pipe)
        at libcore.io.Posix.writeBytes(Native Method)
        at libcore.io.Posix.write(Posix.java:202)
        at libcore.io.BlockGuardOs.write(BlockGuardOs.java:197)
        at libcore.io.IoBridge.write(IoBridge.java:450)
        at java.io.FileOutputStream.write(FileOutputStream.java:187)
        at java.io.OutputStream.write(OutputStream.java:82)
        at java.io.DataOutputStream.writeBytes(DataOutputStream.java:156)

代替

Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});

我试过:

Runtime.getRuntime().exec("su");

它没有帮助。

此代码也适用于我的 root 平板电脑,但不适用于我的电视盒。

我通过以下方法检查 root 上的设备:

private static boolean isRooted() {
    return findBinary("su");
}

public static boolean findBinary(String binaryName) {
    boolean found = false;
    if (!found) {
        String[] places = {"/sbin/", "/system/bin/", "/system/xbin/", "/data/local/xbin/",
                "/data/local/bin/", "/system/sd/xbin/", "/system/bin/failsafe/", "/data/local/"};
        for (String where : places) {
            Log.d(TAG, "DATA where = " + where);
            if ( new File( where + binaryName ).exists() ) {
                found = true;
                break;
            }
        }
    }
    return found;
}

它返回真。

最佳答案

我找到了一个解决方案。问题是我的设备有另一个命令而不是“su”命令

关于安卓。写入失败 : EPIPE (Broken pipe) when use Process,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32501634/

相关文章:

android - 判断设备是否正在运行 MIUI 软件

android - 标准 9 个阴影补丁生成器生成 9 个无法编译的补丁

c# - 如何在ASP.net网页的服务器端使用文本文件读写?

node.js - 使用 nohup/supervise 命令的日志重定向问题

javascript - Node.js-事件.js :154 throw err write EPIPE; Program Crashing

io - 从模板执行中过滤掉损坏的管道错误

android - Kivy Apk build with buildozer error : # Java compiler (javac) not found, 请安装它

android - 使用 Scenform 将 SceneView 加载到 fragment 中后出现黑屏

java - 在 Java 中生成和解析文本文件

Php 浏览一个文件然后获取它的路径