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

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

问题描述

我想从资产的文件夹,但没有成功安装APK编程,请大家帮帮我。我使用下面的code为。谢谢你。

 意向意图=新的意图(Intent.ACTION_VIEW)
.setData(Uri.parse(文件:///android_asset/youtuberanjit.apk))
.setType(应用/ vnd.android.package存档);
startActivity(意向);
 

解决方案

  AssetManager assetManager = getAssets();

在的InputStream = NULL;
出的OutputStream = NULL;

尝试 {
    在= assetManager.open(myapk.apk);
    OUT =新的FileOutputStream(/ SD卡/ myapk.apk);

    byte []的缓冲区=新的字节[1024];

    INT读取;
    而((读= in.read(缓冲))!= -1){

        out.write(缓冲,0,读);

    }

    附寄();
    在= NULL;

    了out.flush();
    out.close();
    OUT = NULL;

    意向意图=新的意图(Intent.ACTION_VIEW);

    intent.setDataAndType(Uri.fromFile(新文件(/ SD卡/ myapk.apk)),
        应用程序/ vnd.android.package存档);

    startActivity(意向);

}赶上(例外五){}
 

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) { }

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

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