FileProvider的使用及應用更新時提示:解析包出錯、失敗等問題

Android 7.0以上的版本更新採用系統自帶的DownloadManager更新html

DOWNLOADPATH ="/download/"  

https://www.jianshu.com/p/55eae30d133c

<external-path name="name" path="path" />
物理路徑至關於Environment.getExternalStorageDirectory() + /path/。
<external-files-path name="name" path="path" />
物理路徑至關於**Context.getExternalFilesDir(String) **+ /path/。

https://www.cnblogs.com/fengfenghuifei/p/9870143.html

這個博主的原文
Manifest裏聲明provider ,xml設置文件目錄,代碼裏獲取uri,intent裏設置uri,而後使用
我這裏要說的是咱們如何設置二級目錄
由於xml裏各類path的構建只容許出現一級目錄的如 TestAPKPath,這樣的
不容許出現TestAPKPath/Img這樣
因此方法就是在構建文件的時候,在文件的目錄上寫上二級域名
File file = new File(「xxxx/TestApkPath/Img」);
Uri uri = FileProvider.getUri("xxx.xxx.provider",file);
這樣咱們就完成了將file存儲到二級目錄的效果

 

  我這裏遇到的問題就是,以前將更新apk包下載到了/jingyeapp/downloadApk  文件夾下了,可是在xml配置時,添加二級目錄,就會合並在一塊,出現解析失敗的問題。android

上面博客的方法,本身未測試。直接修改了新apk包的下載路徑到手機系統的/download/文件夾下。網絡

 

private void initDownManager() {
        LogUtil.d(TAG, "initDownManager");
        manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        receiver = new DownloadCompleteReceiver();
        //設置下載地址
        DownloadManager.Request down = new DownloadManager.Request(Uri.parse(url));
        // 設置容許使用的網絡類型,這裏是移動網絡和wifi均可以
        down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
                | DownloadManager.Request.NETWORK_WIFI);
        down.setAllowedOverRoaming(false);
        MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
        String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
        down.setMimeType(mimeString);
        // 下載時,通知欄顯示途中
        down.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
        // 顯示下載界面
        down.setVisibleInDownloadsUi(true);
        // 設置下載後文件存放的位置
        down.setDestinationInExternalPublicDir(DOWNLOADPATH, "jingye.apk");
        down.setTitle("敬業升級");
        // 將下載請求放入隊列
        manager.enqueue(down);
        //註冊下載廣播
        registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        LogUtil.d(TAG, "onStartCommand");
        url = intent.getStringExtra("downloadurl");
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + DOWNLOADPATH + "jingye.apk";
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
        try {
            // 調用下載
            initDownManager();
            Toast.makeText(getApplicationContext(), "升級中,能夠在通知欄查看進度", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "下載失敗", Toast.LENGTH_SHORT).show();
        }
        return Service.START_NOT_STICKY;
    }  

 

 

public void install(Context context) {
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) ,"jingye.apk");
            Intent intent = new Intent(Intent.ACTION_VIEW);
            //參數1 上下文, 參數2 Provider主機地址 和配置文件中保持一致   參數3  共享的文件
            Uri apkUri =
                    FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID+".fileprovider",file);

            intent.addCategory("android.intent.category.DEFAULT");
            // 因爲沒有在Activity環境下啓動Activity,設置下面的標籤
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //添加這一句表示對目標應用臨時受權該Uri所表明的文件
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");

            LogUtil.d(TAG, "install -apkUri:"+apkUri);
            context.startActivity(intent);

        }

 

 

 

 第一開始,更新時,總提示:安裝包解析失敗,發現路徑跟指望設置的不一樣。app

 

 

正確的路徑ide

相關文章
相關標籤/搜索