解析错误 解析包时出现问题

life02 2012-05-09 02:37:22
求问下段代码有什么问题,为什么老出现以上错误:

String apkPath = "/data/data/" + mContext.getPackageName() + "/files";
String destapkName = "well.apk";
File file = null;
file = new File(apkPath, destapkName);
try {

if (!file.exists()) {
file.createNewFile();
}
FileOutputStream os = mContext.openFileOutput(file.getName(),
Context.MODE_WORLD_READABLE);
byte[] bytes = new byte[512];
int i = -1;
while ((i = is.read(bytes)) > 0) {
os.write(bytes);
}

os.close();
is.close();
String permission = "666";

try {

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

} catch (Exception e) {
Log.e("----", e.toString());
}
if (DEBUG)
Log.e("",
"fl--" + file.getName() + "----"
+ file.getAbsolutePath() + "---"
+ file.getPath());
if (file != null) {
Intent intentInstall = new Intent();
intentInstall
.setAction(android.content.Intent.ACTION_VIEW);
intentInstall.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
intentInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intentInstall);
}
...全文
1446 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
热爱生活 2014-05-21
  • 打赏
  • 举报
回复
谢谢分享!!!!!!!!!!!!!!!!!!!!!
xmoon1983 2013-07-23
  • 打赏
  • 举报
回复
解析错误-解析包时出现问题-解决方法。 http://blog.chinaunix.net/uid-22985736-id-2977687.html 主要是用openFileOutput("test.apk", MODE_PRIVATE)创建输出流造成的。 将文件创建模式MODE_PRIVATE改成MODE_WORLD_READABLE就可以了。 参考:http://stackoverflow.com/questions/5503487/android-failed-to-open-zip-archive 或者 在安装apk前修改权限 chmod 777
csdn_2013 2012-12-03
  • 打赏
  • 举报
回复
恭喜LZ已经解决,但请注意代码规范 否则以后会出现莫名的错~ try { ... os.flush(); ... } catch (Exception e) { ... } finally { try{ if(os != null){ os.close(); } }catch (Exception e) { ... } }
shunanlove 2012-12-03
  • 打赏
  • 举报
回复
在哪里flush啊?
life02 2012-05-09
  • 打赏
  • 举报
回复
少了os.flush
在此非常感谢北风兄、小滕
life02 2012-05-09
  • 打赏
  • 举报
回复
log中也没有出现任何提示,下载到sd卡中就不会出现如此问题,疯掉

80,493

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧