1. 程式人生 > >Android開發 android7.0安裝apk檔案的姿勢

Android開發 android7.0安裝apk檔案的姿勢

最近看到一個庫,覺得有點意思,就下載原始碼編譯了一下,結果發現打不開apk包,報錯為:

    //這個庫的地址是:https://github.com/bingoogolapple/BGAUpdate-Android
    //設定了超連結也不變色,還是直接寫出來得了

      Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/cn.bingoogolapple.update.demo/files/apk/BGAUpdateDemo_v1.0.0.apk exposed beyond app through Intent.getData()

圖文:
這裡寫圖片描述

看了下,估計是沒有相容android7.0的原因,這個作者的gradle設定的比較另類,我是第一次見,就沒改,提交了issues,然後呢作者回復我讓我改,估計他手頭沒有7.0的機器吧,我的也是前兩天才升級了…
在這裡把解決步驟記錄一下..
1、在AndroidManifest.xml配置清單的Application中新增

     <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="cn.bingoogolapple.update.demo.fileprovider"
android:grantUriPermissions="true" android:exported="false" > <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>

注意:cn.bingoogolapple.update.demo 是包名。。。
這裡寫圖片描述


2、在res資原始檔下新建目錄xml,在xml目錄下新建file_paths.xml檔案,內容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
    <external-path path="Android/data/cn.bingoogolapple.update.demo/"        name="files_root" />
    <external-path path="." name="external_storage_root" />
    </paths>

注意:cn.bingoogolapple.update.demo 是包名..
這裡寫圖片描述
3、然後就是對安裝apk方法的更改:

        /**
     * 安裝 apk 檔案
     *
     * @param apkFile
     */
    public static void installApk(File apkFile) {
       /* Intent installApkIntent = new Intent();
        installApkIntent.setAction(Intent.ACTION_VIEW);
        installApkIntent.addCategory(Intent.CATEGORY_DEFAULT);
        installApkIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        installApkIntent.setDataAndType(Uri.fromFile(apkFile), MIME_TYPE_APK);

        if (sApp.getPackageManager().queryIntentActivities(installApkIntent, 0).size() > 0) {
            sApp.startActivity(installApkIntent);
        }*/
        //Toast.makeText(sApp,apkFile.getPath(),Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(sApp, "cn.bingoogolapple.update.demo.fileprovider", apkFile);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        if (sApp.getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
            sApp.startActivity(intent);
        }
    }

如圖:

這裡寫圖片描述
如果包名寫錯了會空指標。。。

    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:560)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:534)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:376)

最後結果自然是打開了安裝頁面啦…這個是在專案裡面直接修改的,如果在library裡面修改,步驟是一樣的
這裡寫圖片描述
//////////////////////////////////系統截圖/////////////////////////////////////////////
2016年12月17日23:35:21
這裡寫圖片描述
如果你想下載這個demo來看看,點選下載,不過最好還是看作者的,相信他已經相容了吧,哈
下載地址:
java
作者的github:https://github.com/bingoogolapple/BGAUpdate-Android
當前版本:
http://download.csdn.net/detail/pkandroid/9714442

————————–2017年3月9日16:44:11———————————–
注意:如果AndroidManifest.xml中配置的provider的authorities名稱不能重複,如果在別的APP中使用了這個provide的authorities屬性(比如APP對於Android7.0之後的拍照的適配),那麼這個APP可能會安裝不上,提示解除安裝含有相同provider authorities屬性的APP