从android中的assets文件夹以编程方式安装apk [英] Programatically install apk from assets folder in android

查看:36
本文介绍了从android中的assets文件夹以编程方式安装apk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 assets 文件夹以编程方式安装 apk 但没有成功,请帮助我.我为此使用了以下代码.谢谢.

I am trying to install apk programatically from assets folder but not success, Please help me. I am using following code for that. thank you.

Intent intent = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("file:///android_asset/youtuberanjit.apk"))
.setType("application/vnd.android.package-archive");
startActivity(intent);

推荐答案

AssetManager assetManager = getAssets();

InputStream in = null;
OutputStream out = null;

try {
    in = assetManager.open("myapk.apk");
    out = new FileOutputStream("/sdcard/myapk.apk");

    byte[] buffer = new byte[1024];

    int read;
    while((read = in.read(buffer)) != -1) {

        out.write(buffer, 0, read);

    }

    in.close();
    in = null;

    out.flush();
    out.close();
    out = null;

    Intent intent = new Intent(Intent.ACTION_VIEW);

    intent.setDataAndType(Uri.fromFile(new File("/sdcard/myapk.apk")),
        "application/vnd.android.package-archive");

    startActivity(intent);

} catch(Exception e) { }

这篇关于从android中的assets文件夹以编程方式安装apk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆