美文网首页
利用Glide 完成apk 下载安装 带进度条

利用Glide 完成apk 下载安装 带进度条

作者: 善良的老农 | 来源:发表于2023-03-11 17:25 被阅读0次

    参考地址


     接上文: 几个错误解决如下

    1.Adding support for Java 8 language features could solve this issue.

    kotlinOptions { jvmTarget = 1.8}

    2. 文件的copy 与命名 

    工具类FileUtils 

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileNotFoundException;

    import java.io.FileOutputStream;

    import java.io.IOException;

    /**

    * @author as752497576@gmail.com

    * @description

    * @time 2023/3/12

    */

    public class FileUtils {

        /**

        * 创建文件夹

        */

        public static boolean createDirs(String dirPath) {

            File file = new File(dirPath);

            if (!file.exists() || !file.isDirectory()) {

                return file.mkdirs();

            }

            return true;

        }

        /**

        * 文件的复制操作方法

        *

        * @param fromFile 准备复制的文件

        * @param toFile  要复制的文件的目录

        */

        public static void copyfile(File fromFile, File toFile) {

            if (!fromFile.exists()) {

                return;

            }

            if (!fromFile.isFile()) {

                return;

            }

            if (!fromFile.canRead()) {

                return;

            }

            if (!toFile.getParentFile().exists()) {

                toFile.getParentFile().mkdirs();

            }

            if (toFile.exists()) {

                toFile.delete();

            }

            try {

                FileInputStream fosfrom = new FileInputStream(fromFile);

                FileOutputStream fosto = new FileOutputStream(toFile);

                byte[] bt = new byte[1024];

                int c;

                while ((c = fosfrom.read(bt)) > 0) {

                    fosto.write(bt, 0, c);

                }

                //关闭输入、输出流

                fosfrom.close();

                fosto.close();

            } catch (FileNotFoundException e) {

                e.printStackTrace();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

        /**

        * 重命名文件

        *

        * @param oldPath 原来的文件地址

        * @param newPath 新的文件地址

        */

        public static void renameFile(String oldPath, String newPath) {

            File oleFile = new File(oldPath);

            File newFile = new File(newPath);

            //执行重命名

            oleFile.renameTo(newFile);

        }

        /**

        * 删除文件夹

        *

        * @return boolean

        */

        public static void delFolder(String folderPath) {

            try {

                delAllFile(folderPath); //删除完里面所有内容

                String filePath = folderPath;

                filePath = filePath.toString();

                File myFilePath = new File(filePath);

                myFilePath.delete(); //删除空文件夹

            } catch (Exception e) {

                System.out.println("删除文件夹操作出错");

                e.printStackTrace();

            }

        }

        /**

        * 删除文件夹里面的所有文件

        *

        * @param path String 文件夹路径 如 c:/fqf

        */

        public static void delAllFile(String path) {

            File file = new File(path);

            if (!file.exists()) {

                return;

            }

            if (!file.isDirectory()) {

                return;

            }

            String[] tempList = file.list();

            File temp = null;

            for (int i = 0; i < tempList.length; i++) {

                if (path.endsWith(File.separator)) {

                    temp = new File(path + tempList[i]);

                } else {

                    temp = new File(path + File.separator + tempList[i]);

                }

                if (temp.isFile()) {

                    temp.delete();

                }

                if (temp.isDirectory()) {

                    delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件

                    delFolder(path + "/" + tempList[i]);//再删除空文件夹

                }

            }

        }

    }

    val fileName ="huipijiang" +".apk"

    val cacheDir = BaseApplication.mContext.getDownLoadPath()

    FileUtils.createDirs(cacheDir)

    var  path  =    File(cacheDir + File.separator + fileName)

    FileUtils.copyfile(resource,path  )

    FileUtils.renameFile(resource.toString(),path.toString()  ) 

    FileUtils.delFolder(resource.toString()); // 删除下载的文件

    SilentInstall.install(File(cacheDir + File.separator + fileName),BaseApplication.getContext())  //安装apk 

    getDownLoadPath()代码

    fun getDownLoadPath(): String {

    return if (Build.VERSION.SDK_INT >29) {

    getExternalFilesDir(null)?.absolutePath + File.separator +"BifrostApkDownload"

        }else {

    Environment.getExternalStorageDirectory().absolutePath + File.separator +"BifrostApkDownload"

        }

    }

    安装代码: 

    Intent intent =new Intent(Intent.ACTION_VIEW); 

    intent.setAction(Intent.ACTION_VIEW);

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

    intent.addCategory(Intent.CATEGORY_DEFAULT);

    Uri uri;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 

        uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() +".fileProvider", apk);

        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    }else { 

        uri = Uri.fromFile(apk);

    }

    intent.setDataAndType(uri, "application/vnd.android.package-archive");

    context.startActivity(intent);

    相关文章

      网友评论

          本文标题:利用Glide 完成apk 下载安装 带进度条

          本文链接:https://www.haomeiwen.com/subject/dzvxrdtx.html