利用Glide 完成apk 下載安裝 帶進(jìn)度條

參考地址


?接上文: 幾個(gè)錯(cuò)誤解決如下

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 {

? ? /**

? ? * 創(chuàng)建文件夾

? ? */

? ? public static boolean createDirs(String dirPath) {

? ? ? ? File file = new File(dirPath);

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

? ? ? ? ? ? return file.mkdirs();

? ? ? ? }

? ? ? ? return true;

? ? }

? ? /**

? ? * 文件的復(fù)制操作方法

? ? *

? ? * @param fromFile 準(zhǔn)備復(fù)制的文件

? ? * @param toFile? 要復(fù)制的文件的目錄

? ? */

? ? 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);

? ? ? ? ? ? }

? ? ? ? ? ? //關(guān)閉輸入、輸出流

? ? ? ? ? ? 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);

? ? ? ? //執(zhí)行重命名

? ? ? ? oleFile.renameTo(newFile);

? ? }

? ? /**

? ? * 刪除文件夾

? ? *

? ? * @return boolean

? ? */

? ? public static void delFolder(String folderPath) {

? ? ? ? try {

? ? ? ? ? ? delAllFile(folderPath); //刪除完里面所有內(nèi)容

? ? ? ? ? ? String filePath = folderPath;

? ? ? ? ? ? filePath = filePath.toString();

? ? ? ? ? ? File myFilePath = new File(filePath);

? ? ? ? ? ? myFilePath.delete(); //刪除空文件夾

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? System.out.println("刪除文件夾操作出錯(cuò)");

? ? ? ? ? ? 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);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容