libaums icon indicating copy to clipboard operation
libaums copied to clipboard

因为无法获取真实路径,我应该如何使用libaums安装U盘中的apk,copy?不能直接安装吗?

Open Fly-Felix opened this issue 3 years ago • 7 comments

Problem

Expected behavior

Actual behavior

Stacktrace of Excpetion

(if exists)

Code where problem occurs

Fly-Felix avatar Dec 07 '21 06:12 Fly-Felix

是的,复制到内部存储然后使用FileProvider获取Uri

zongren avatar Dec 09 '21 03:12 zongren

好的,谢谢

---原始邮件--- 发件人: @.> 发送时间: 2021年12月9日(周四) 中午11:09 收件人: @.>; 抄送: @.@.>; 主题: Re: [magnusja/libaums] 因为无法获取真实路径,我应该如何使用libaums安装U盘中的apk,copy?不能直接安装吗? (Issue #325)

是的,复制到内部存储然后使用FileProvider获取Uri

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

Fly-Felix avatar Dec 09 '21 03:12 Fly-Felix

好的谢谢

---原始邮件--- 发件人: @.> 发送时间: 2021年12月9日(周四) 中午11:09 收件人: @.>; 抄送: @.@.>; 主题: Re: [magnusja/libaums] 因为无法获取真实路径,我应该如何使用libaums安装U盘中的apk,copy?不能直接安装吗? (Issue #325)

是的,复制到内部存储然后使用FileProvider获取Uri

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

Fly-Felix avatar Dec 09 '21 03:12 Fly-Felix

可直接在u盘安装apk!!! 系统安装方法实际也是先通过读写IO流copy apk,那么我们也可以通过new UsbFileInputStream(UsbFile)获取到IO流,再在系统方法基础上替换获取IO流的部分即可.

代码如下: @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public static void install28UsbFile(Context context, UsbFile apkFile,InstallCallback callback) { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); PackageInstaller.SessionParams sessionParams = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL); sessionParams.setSize(apkFile.getLength());

    int sessionId = createSession(packageInstaller, sessionParams);
    if (sessionId != -1) {
        boolean copySuccess = copyInstallUsbFile(packageInstaller, sessionId, apkFile);
        if (copySuccess) {
            execInstallCommand(context, packageInstaller, sessionId,apkFile.getName());
        }else {
            if (callback !=null)
                callback.onFailure(apkFile.getName());
        }
    }else {
        if (callback !=null)
            callback.onFailure(apkFile.getName());
    }
}



@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private static boolean copyInstallUsbFile(PackageInstaller packageInstaller,
                                       int sessionId, UsbFile apkFile) {
    InputStream in = null;
    OutputStream out = null;
    PackageInstaller.Session session = null;
    boolean success = false;
    try {
        session = packageInstaller.openSession(sessionId);
        out = session.openWrite("base.apk", 0, apkFile.getLength());
        in = new UsbFileInputStream(apkFile);
        int total = 0, c;
        byte[] buffer = new byte[65536];
        while ((c = in.read(buffer)) != -1) {
            total += c;
            out.write(buffer, 0, c);
        }
        session.fsync(out);
        Log.d(TAG, "streamed " + total + " bytes");
        success = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeQuietly(out);
        closeQuietly(in);
        closeQuietly(session);
    }
    return success;
}



@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private static void execInstallCommand(Context context, PackageInstaller packageInstaller, int sessionId,String apkFilePath) {
    PackageInstaller.Session session = null;
    try {
        session = packageInstaller.openSession(sessionId);
        Intent intent = new Intent(context, InstallResultReceiver.class);
        intent.putExtra("apkFilePath",apkFilePath);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        session.commit(pendingIntent.getIntentSender());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeQuietly(session);
    }
}

656643899 avatar Jan 14 '22 08:01 656643899

可直接在u盘安装apk!!! 系统安装方法实际也是先通过读写IO流copy apk,那么我们也可以通过new UsbFileInputStream(UsbFile)获取到IO流,再在系统方法基础上替换获取IO流的部分即可.

代码如下: @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public static void install28UsbFile(Context context, UsbFile apkFile,InstallCallback callback) { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); PackageInstaller.SessionParams sessionParams = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL); sessionParams.setSize(apkFile.getLength());

    int sessionId = createSession(packageInstaller, sessionParams);
    if (sessionId != -1) {
        boolean copySuccess = copyInstallUsbFile(packageInstaller, sessionId, apkFile);
        if (copySuccess) {
            execInstallCommand(context, packageInstaller, sessionId,apkFile.getName());
        }else {
            if (callback !=null)
                callback.onFailure(apkFile.getName());
        }
    }else {
        if (callback !=null)
            callback.onFailure(apkFile.getName());
    }
}



@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private static boolean copyInstallUsbFile(PackageInstaller packageInstaller,
                                       int sessionId, UsbFile apkFile) {
    InputStream in = null;
    OutputStream out = null;
    PackageInstaller.Session session = null;
    boolean success = false;
    try {
        session = packageInstaller.openSession(sessionId);
        out = session.openWrite("base.apk", 0, apkFile.getLength());
        in = new UsbFileInputStream(apkFile);
        int total = 0, c;
        byte[] buffer = new byte[65536];
        while ((c = in.read(buffer)) != -1) {
            total += c;
            out.write(buffer, 0, c);
        }
        session.fsync(out);
        Log.d(TAG, "streamed " + total + " bytes");
        success = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeQuietly(out);
        closeQuietly(in);
        closeQuietly(session);
    }
    return success;
}



@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private static void execInstallCommand(Context context, PackageInstaller packageInstaller, int sessionId,String apkFilePath) {
    PackageInstaller.Session session = null;
    try {
        session = packageInstaller.openSession(sessionId);
        Intent intent = new Intent(context, InstallResultReceiver.class);
        intent.putExtra("apkFilePath",apkFilePath);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        session.commit(pendingIntent.getIntentSender());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeQuietly(session);
    }
}

这种方式还是复制到临时文件,然后再安装吧?

zongren avatar Jan 15 '22 01:01 zongren

谢谢,这种方法是可行的,十分感谢

---原始邮件--- 发件人: @.> 发送时间: 2022年1月14日(周五) 下午4:34 收件人: @.>; 抄送: @.@.>; 主题: Re: [magnusja/libaums] 因为无法获取真实路径,我应该如何使用libaums安装U盘中的apk,copy?不能直接安装吗? (Issue #325)

可直接在u盘安装apk!!! 系统安装方法实际也是先通过读写IO流copy apk,那么我们也可以通过new UsbFileInputStream(UsbFile)获取到IO流,再在系统方法基础上替换获取IO流的部分即可.

代码如下: @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public static void install28UsbFile(Context context, UsbFile apkFile,InstallCallback callback) { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); PackageInstaller.SessionParams sessionParams = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL); sessionParams.setSize(apkFile.getLength()); int sessionId = createSession(packageInstaller, sessionParams); if (sessionId != -1) { boolean copySuccess = copyInstallUsbFile(packageInstaller, sessionId, apkFile); if (copySuccess) { execInstallCommand(context, packageInstaller, sessionId,apkFile.getName()); }else { if (callback !=null) callback.onFailure(apkFile.getName()); } }else { if (callback !=null) callback.onFailure(apkFile.getName()); } } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private static boolean copyInstallUsbFile(PackageInstaller packageInstaller, int sessionId, UsbFile apkFile) { InputStream in = null; OutputStream out = null; PackageInstaller.Session session = null; boolean success = false; try { session = packageInstaller.openSession(sessionId); out = session.openWrite("base.apk", 0, apkFile.getLength()); in = new UsbFileInputStream(apkFile); int total = 0, c; byte[] buffer = new byte[65536]; while ((c = in.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); } session.fsync(out); Log.d(TAG, "streamed " + total + " bytes"); success = true; } catch (IOException e) { e.printStackTrace(); } finally { closeQuietly(out); closeQuietly(in); closeQuietly(session); } return success; } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private static void execInstallCommand(Context context, PackageInstaller packageInstaller, int sessionId,String apkFilePath) { PackageInstaller.Session session = null; try { session = packageInstaller.openSession(sessionId); Intent intent = new Intent(context, InstallResultReceiver.class); intent.putExtra("apkFilePath",apkFilePath); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); session.commit(pendingIntent.getIntentSender()); } catch (IOException e) { e.printStackTrace(); } finally { closeQuietly(session); } }
— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you authored the thread.Message ID: @.***>

Fly-Felix avatar Jan 15 '22 02:01 Fly-Felix

@FlyFelix 请问打开速度有提升吗

zongren avatar Jan 15 '22 02:01 zongren