我在項目中使用了 compile ‘com.jph.takephoto:takephoto_library:4.0.3’ 這個依賴 也是最新的依賴,然後我要做版本升級,就是下載好新的apk的時候去跳轉安裝,爲啦適配7.0以上的機型做了如下的操作
對於7.0以上的 我們要先共享某個文件夾,才能使用裏面的文件,
file_paths的內容如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<!--name 是起的別名(隨便起) path是要共享的文件夾 MeangerNew-->
<external-path
name="files_root"
path="Android/data/我的包名/" />
<external-path
name="external_storage_root"
path="." />
</paths>
</resources>
在清單文件中加入代碼
//這裏本來應該是 android:name="android.support.v4.content.FileProvider" 稍後解釋
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="我的包名.fileProvider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="name,authorities,exported,grantUriPermissions">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"
tools:replace="name,resource" />
</provider>
Intent intent=new Intent(Intent.ACTION_VIEW);
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N) {
//file 就是下載好的apk安裝包
Uri contentUri = FileProvider.getUriForFile(this,"我的包名.fileProvider",file);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(contentUri,"application/vnd.android.package-archive");
}else{
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
}
//跳轉到安裝的界面
this.startActivity(intent);
此時運行代碼 發現報錯如下
根據提示在上面節點中 加入這句話
本來加入到了 application的節點下 但是發現,又會報如下錯誤
不知如何解決了 最後吧這句話加入到了provider的節點下,編譯通過,不報錯了
並且下載好的安裝包也能跳轉到安裝界面了,但是會發現 這個頭像的功能就不能使用了,原因好像是這句話 tools:replace=”name,authorities,exported,grantUriPermissions”,意思就是 替換成了自己的共享目錄,所以導致了頭像功能失效,於是陷入了 更新不能用頭像 用頭像不恩能夠更新的地步,最後百度了好久終於 找到了解決方案;
先是下載了takephoto_library的源碼找到了原因
我發現takephoto_library中也使用了這樣的註冊信息
//takephoto_library中的清單文件信息
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jph.takephoto" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true">
<activity android:name="com.soundcloud.android.crop.CropImageActivity" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
文件信息代碼
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<root-path path="" name="camera_photos" />
</paths>
</resources>
即使我吧自己的文件名字改成和他不一樣的 也依然會報錯,因爲主要原因不是因爲 文件名衝突,而是清單文件中的
android:name=”android.support.v4.content.FileProvider”這一句話衝突
於是我在自己項目中繼承了android.support.v4.content.FileProvider 未做任何的處理 改爲自己的類
將自己的清單文件中的註冊 改爲如下
<provider
android:name="包路徑.MyFileProvider"
android:authorities="我的包名.fileProvider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="name,authorities,exported,grantUriPermissions">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"
tools:replace="name,resource" />
</provider>
讓後編譯 運行 完美解決
本篇僅供個人參考 沒時間寫,寫的亂七八糟的,自己記錄一下,而已 如果你發現 takephoto_library還和其他的 三方衝突,請參考這篇代碼,
http://blog.csdn.net/liuyu0915/article/details/72621905
希望這篇亂糟糟的文章能夠幫到你!!!我這是個人做筆記,見諒