先上代碼
public static boolean install(Context con, String filePath) {
try {
if(TextUtils.isEmpty(filePath))
return false;
File file = new File(filePath);
if(file.exists()){
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//增加讀寫權限
}
intent.setDataAndType(getPathUri(con, filePath), "application/vnd.android.package-archive");
con.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(con, "安裝失敗,請重新下載", Toast.LENGTH_LONG).show();
return false;
} catch (Error error) {
error.printStackTrace();
Toast.makeText(con, "安裝失敗鸠信,請重新下載", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
public static Uri getPathUri(Context context, String filePath) {
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String packageName = context.getPackageName();
uri = FileProvider.getUriForFile(context, packageName + ".fileProvider", new File(filePath));
} else {
uri = Uri.fromFile(new File(filePath));
}
return uri;
}
由于Android.N做了修改需要添加權限
1、在AndroidManifest中添加provider和權限
<application
...>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
2纺酸、在res下創(chuàng)建xml文件夾,接著在xml文件夾中傳教file_paths.xml文件
<?xml version="1.0" encoding="utf-8"?>
<paths>
<root-path name="root" path="" />
<external-path name="external_storage_root" path="." />
<external-path name="external_storage_download" path="Download" />
</paths>
說一下FileProvider
隨著Android 7.0的到來引入“私有目錄被限制訪問”恕洲,“StrictMode API 政策”霜第,為了進一步提高私有文件的安全性,Android不再由開發(fā)者放寬私有文件的訪問權限秀睛,之前我們一直使用”file:///”絕對路徑來傳遞文件地址的方式蛮拔,在接收方訪問時很容易觸發(fā)SecurityException的異常。
因此酝掩,為了更好的適配Android 7.0,例如相機拍照這類涉及到文件地址傳遞的地方就用上了FileProvider眷柔,F(xiàn)ileProvider也更好地進入了大家的視野期虾。
其實FileProvider是ContentProvider的一個特殊子類,本質上還是基于ContentProvider的實現(xiàn)驯嘱,F(xiàn)ileProvider會把”file:///”的路徑轉換為特定的”content://”形式的content uri镶苞,接收方通過這個uri再使用ContentResolver去媒體庫查詢解析。
說一下file_paths.xml文件<paths>中可以定義的子節(jié)點
file://到content://的轉換規(guī)則:
1.替換前綴:把file://替換成content://${android:authorities}鞠评。
2.匹配和替換
2.1.遍歷的子節(jié)點茂蚓,找到最大能匹配上文件路徑前綴的那個子節(jié)點。
2.2.用path的值替換掉文件路徑里所匹配的內容。
3.文件路徑剩余的部分保持不變聋涨。
注意
1晾浴、為了兼容8.0手機需要添加權限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
2、文件的路徑必須包含在xml中牍白,也就是2.1中必須能找到一個匹配的子節(jié)點
3脊凰、發(fā)現(xiàn)在華為mate9升級到Android8.0后,一直提示”安裝解析包錯誤”茂腥,這個問題就是個例狸涌,我反饋給參考文章中的大牛,他反饋給華為內部最岗,他們已經解決杈抢,升級到最新ROM就可以了。
參考文章
解決 Android N 上報錯:android.os.FileUriExposedException: file:///storage/emulated/0/
華為android6.0仑性,代碼安裝apk惶楼,不會彈出安裝界面
安卓7.0遇到 android.os.FileUriExposedException: file:///storage/emulated.. exposed beyond app through Intent.getData()
對Provider詳解