apk里安装apk [更新代码]

假设在Aapk中放入Bapk,在Aapk安装运行后,要安装Bapk

将Bapk放在raw目录。

将Bapk拷贝至/data/data/Aapk的包名/files

设置Bapk的权限。

通过系统安装器安装。

代码随后上

String apkPath = "/data/data/" + getPackageName() + "/files";
String apkName = "b.apk";
File file = new File(apkPath,apkName);

try
{
	            InputStream is = getResources().openRawResource(R.raw.b);

	            if(!file.exists())
	            {
		            file.createNewFile();

		            FileOutputStream os = openFileOutput(file.getName(), Context.MODE_WORLD_WRITEABLE);

		            byte[] bytes = new byte[512];
		            int i = -1;
		            while((i = is.read(bytes))>0)
		            {
			            os.write(bytes);
		            }

		            os.close();
		            is.close();
		            Log.d(LOG_TAG, apkName + " has been copy to " + apkPath);
	            };

	            String permission="666";

	            try
	            {
	                String command = "chmod " + permission + " " + apkPath + "/" + apkName;
	                Runtime runtime = Runtime.getRuntime();
	                runtime.exec(command);
	            }
	            catch (IOException e)
	            {
	                e.printStackTrace();
	            }

	        }
            catch(Exception e)
            {
	            Log.d(LOG_TAG, e.toString());
	            finish();
            }

            Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);

如果apk文件过大,如下

-------------------------------------------------------

http://www.hfdigg.com/SrcShow.asp?Src_ID=10092

androidraw文件夹下.db后缀文件大于1M时,拷贝时将会出现:DEBUG/asset(725):DataexceedsUNCOMPRESS_DATA_MAX(1662976vs1048576)

出现这个问题的原因是,assetsManager无法处理大于1M的文件的压缩和解压。

但以下文件类型,因为是已经压缩过的,不会进行压缩处理,如下:

/*theseformatsarealreadycompressed,ordontcompresswell*/

staticconstchar*kNoCompressExt[]={

".jpg",".jpeg",".png",".gif",

".wav",".mp2",".mp3",".ogg",".aac",

".mpg",".mpeg",".mid",".midi",".smf",".jet",

".rtttl",".imy",".xmf",".mp4",".m4a",

".m4v",".3gp",".3gpp",".3g2",".3gpp2",

".amr",".awb",".wma",".wmv"

};

【解决办法】将Sqlitedb文件,先改名为.jpg文件,放在assets中,然后在程序第一次启动时,改名拷贝到/data/data

相关推荐