Android7.0機型適配存儲目錄的共享
都知道現在AI都很火,AI必定會誕生一些下一代巨頭企業,各行各業都爭搶這一股風,百度CEO不扔下了公司的內務不管,一頭扎進了AI的研究團隊工作中,前段時間李彥宏駕著自己公司生產的無人駕駛汽車,就是6,各個大巨頭都在AI方面有重要項目在實施,都爭搶著下一個時代的山頭。在物聯網方面,做的不錯的是小米,小米的生態鏈,聚集了70多家智能硬件公司,形成了小米生態鏈,未來的小米將會在物聯網將是一個大贏家,我們知道的小米路由器,小米手環,小米掃地機器人等都是這70多家公司中來生產和研發的,雷軍太有遠見了。好了,不閑說了,進入正題,記錄一下我嗎7.0版本對使用存儲的要求。
最近根據需求做了在線更新app問題,起初沒有對7.0以上的機型進行
適配,造成7.0手機無法跳轉到app安裝界面。
說一下吧,android系統發展越來越好用,用戶體驗越來越好,對
用戶的隱私也進行了一定規范性的保護,我們都知道從6.0系統的開始
goole就開始對app使用手機權限進行了一定的約束,對于6.0以上
的權限申請我就不多說了,對于權限申請,自己寫了一個框架,在項
目中使用很方便。下面我們就說說7.0系統對存儲使用增加
了“FileProvider”,我們來看看使用方式,閱讀一下官方文檔。
FileProvider介紹
A content URI allows you to grant read and write access using temporary access permissions. When
you create an Intent containing a contentURI, in
order to send the content URI to a client app, you can also call Intent.setFlags() to add permissions. These permissions are available to the client app for as long as the stack for a receiving Activity is active. For an Intent going to a Service, the permissions are available as long as the Service is running.
The increased level of file access security offered by a content URI makes FileProvider a key part of Android's security infrastructure.
This overview of FileProvider includes the following topics:
FileProvider是一個特殊的ContentProvider子類,它通過為文件創建一個Content:/ / Uri而不是file:/ / Uri,從而促進與應用程序關聯的文件的安全共享。
相比之下,為了控制對文件的file:/ / / Uri,您必須修改底層文件的文件系統權限。您提供的權限對任何應用程序都可用,并且一直有效,直到您更改它們為止。這種級別的訪問基本上是不安全的。
由內容URI提供的文件訪問安全性的提高使得FileProvider成為Android安全基礎設施的關鍵部分。
使用方式
1.在程序清單文件中添加代碼:
...? ...? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ...
com.mydomain更換成自己項目的包名,唯一性。
2.在res目錄下新建一個xml文件夾,文件下新建一個xml文件“file_path”
3.file_path文件寫法
? ?
根據我們使用的存儲路徑不同,對第三層的配置也不一樣。
(1)當我們使用Context.getFilesDir()獲取路徑時我們應該配 置成 (2)當我們使用getCacheDir()獲取存儲路徑時,應該配置成 (3)使用Environment.getExternalStorageDirectory() 獲取目錄時,,path值可以為null,null代表授權這個目錄下的所有文件,name必須寫,不然會報嚴重異常 (4)使用Context.getExternalCacheDir() (5)使用Context.getExternalFilesDir(null)配置成
另外,我們在跳轉到安裝目錄代碼有變如下:
public static void install(Context context,String target) {? ? File file = new File(target);? ? Intent intent = new Intent(Intent.ACTION_VIEW);? ? // 由于沒有在Activity環境下啟動Activity,設置下面的標簽? ? intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);if(Build.VERSION.SDK_INT>=24) { //判讀版本是否在7.0以上? ? ? ? //參數1 上下文, 參數2 Provider主機地址 和配置文件中保持一致? 參數3? 共享的文件? ? ? ? Uri apkUri =? ? ? ? ? ? ? ? FileProvider.getUriForFile(context,"com.gzzhe.zhhwlkj.zigou.fileprovider", file);? ? ? ? //添加這一句表示對目標應用臨時授權該Uri所代表的文件? ? ? ? intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);? ? ? ? intent.setDataAndType(apkUri,"application/vnd.android.package-archive");? ? }else{? ? ? ? intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");? ? }? ? context.startActivity(intent);}
好了就介紹到這兒吧。