zoukankan      html  css  js  c++  java
  • android 内部存储 安装apk

    在做应用自动更新模块下载apk时遇到了内部存储和sd卡存储两种情况,存在sk卡中存储apk可以正常安装,可是在内部存储中安装apk时出现了parse error的问题。

    在网上搜了搜,大致分为两种方案:

    1、在存储时给文件设定权限

    2、在使用文件之前更改文件权限

    起初思路并没有理清,就开始尝试,多次尝试之后问题仍没有解决,再请教了大牛之后才开始一点点分析。

    首先使用普通的文件读写

     

     

    File apkFile = new File(mSavePath, appName);
    FileOutputStream fos = new FileOutputStream(apkFile);


    然后使用方案2:

     

     

    String chmodCmd = "chmod 666 " + apkfile.getAbsolutePath();
    try {
                Runtime.getRuntime().exec(chmodCmd);
    } catch (Exception e) {
    }
    Intent i = new Intent(Intent.ACTION_VIEW);        
    String filePath = "file://" + apkfile.toString();
    i.setDataAndType(Uri.fromFile(apkfile),"application/vnd.android.package-archive");
    mContext.startActivity(i);


    问题解决了。

    回过头来看方案一问什么不起作用,当我看文件时很吃惊,命名文件是下载下俩了,可是调用完了以后文件大小为0了,发现FileOutputStream fos = mContext.openFileOutput(appName,Context.MODE_WORLD_READABLE| Context.MODE_WORLD_WRITEABLE);在存文件和调用apk安装代码之前分别使用了一次,openFileOutput方法再次调用导致文件内容被清空,只需要在写文件的时候把文件权限置为读写权限便可。


     

    String fileName ="tmp.apk";FileOutputStream fos = openFileOutput(fileName,
            MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);// write the .apk content here ... flush() and close()// Now start the standard instalation windowFile fileLocation =newFile(context.getFilesDir(), fileName);Intent intent =newIntent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(fileLocation),"application/vnd.android.package-archive");
    context.startActivity(intent);


    参考:

    http://stackoverflow.com/questions/8784404/download-and-install-an-application-apk-from-internal-memory-no-sd-card

  • 相关阅读:
    socket
    RBAC
    CMOS和BIOS
    canvas和SVG
    兼容IE,chrome,ff的设为首页、加入收藏及保存到桌面
    HTML标签marquee实现滚动效果
    百度判断手机终端并自动跳转uaredirect.js代码及使用实例
    JavaScript中常用的事件
    解决windows server 2008 r2 登录进入桌面只显示一片蓝色背景
    ng2自定义管道
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3244025.html
Copyright © 2011-2022 走看看