偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Android 7.0動(dòng)態(tài)權(quán)限大總結(jié)

移動(dòng)開(kāi)發(fā) Android
應(yīng)公司項(xiàng)目需求,做了下Android 7.0適配。對(duì)于我們程序員,適配7.0主要就是對(duì)手機(jī)本地文件的Uri做轉(zhuǎn)換處理。注意紅色字體,意思也就是說(shuō)對(duì)于http開(kāi)頭的等等,非手機(jī)存儲(chǔ)中的文件就不用管了。

應(yīng)公司項(xiàng)目需求,做了下Android 7.0適配。對(duì)于我們程序員,適配7.0主要就是對(duì)手機(jī)本地文件的Uri做轉(zhuǎn)換處理。注意紅色字體,意思也就是說(shuō)對(duì)于http開(kāi)頭的等等,非手機(jī)存儲(chǔ)中的文件就不用管了。Uri.parse(“package”) 這樣的也不用管!!!!!

7.0的適配,就是對(duì)手機(jī)存儲(chǔ)中的私有文件路徑的保護(hù),當(dāng)系統(tǒng)發(fā)現(xiàn)你通過(guò)intent帶走了一個(gè)uri,地址是本地的文件,就會(huì)限制的。其他的原理普及請(qǐng)搜索其他文章吧,此處略。

下面貼一下使用步驟和我的工具類。

一、需要修改當(dāng)前module的AndroidManifest.xml文件,添加provider標(biāo)簽,映射路徑。

  1. <android:supportsRtl="true"
  2.   
  3. <provider 
  4.      android:name="android.support.v4.content.FileProvider" 
  5.      android:authorities="${applicationId}.myFileProvider" 
  6.      android:exported="false" 
  7.      android:grantUriPermissions="true"
  8.      <meta-data 
  9.           android:name="android.support.FILE_PROVIDER_PATHS" 
  10.           android:resource="@xml/path_file" />  

文中紅色部分是固定寫(xiě)法,官網(wǎng)就是這么說(shuō)的,別亂試了。android:resource標(biāo)簽的值是main/res/xml/path_file.xml文件,即在 res下新建xml文件夾,再新建一個(gè)xml文件。

二、在res/xml下新建一個(gè)path_file.xml文件,文件名隨便去,但是與上步驟一致即可。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.   <paths> 
  4.     <!-- 
  5.     <files-path/>代表的根目錄: Context.getFilesDir() 
  6.     <cache-path/>代表的根目錄: getCacheDir() 
  7.     <external-path/>代表的根目錄: Environment.getExternalStorageDirectory() 
  8.     <external-files-path/>代表的根目錄: Context.getExternalFilesDir(String) Context.getExternalFilesDir(null). 
  9.     <external-cache-path />代表的根目錄: Context.getExternalCacheDir(). 
  10.     <root-path />代表設(shè)備的根目錄new File("/"); 
  11.     --> 
  12.     <!-- path=""代表根目錄,也可以指定特定目錄,name="camera_picture"是虛擬目錄camera_picture --> 
  13.     <root-path name="root" path="" /> 
  14.     <files-path name="files" path="" /> 
  15.     <cache-path name="cache" path="" /> 
  16.     <external-path name="external" path="" /> 
  17.     <external-files-path name="external_files" path="" /> 
  18.     <external-cache-path name="external_cache" path="" /> 
  19.   </paths> 
  20. </resources>  

里面共有6個(gè)path標(biāo)簽,含義都寫(xiě)了,根據(jù)你的需要些對(duì)應(yīng)的標(biāo)簽即可。比喻<external-path/>標(biāo)簽,里面的path=”"時(shí),這個(gè)標(biāo)簽映射的就是外掛sd卡根目錄了,name屬性沒(méi)什么用,來(lái)迷惑第三方應(yīng)用的虛擬目錄,來(lái)掩蓋文件的真實(shí)路徑。

三、工具類來(lái)了。

FileUriPermissionCompat.Java

  1. /** 
  2.  * @Author: duke 
  3.  * @DateTime: 2017-06-06 14:43 
  4.  * @Description: android 7.0 uri權(quán)限適配, 
  5.  * (通過(guò)intent暴漏uri或file給第三方app時(shí)的)私有目錄被禁止訪問(wèn) 
  6.  * 已對(duì)local path和net path做了適配 
  7.  */ 
  8. public class FileUriPermissionCompat { 
  9.    private static final String TAG = FileUriPermissionCompat.class.getSimpleName(); 
  10.   
  11.    // TODO: 此處需要更改為對(duì)應(yīng)值 
  12. //此處需要改成AndroidManifest.xml中申請(qǐng)的對(duì)應(yīng)的provider的authorities值 
  13.    private static final String AUTHORITIES = "com.duke.personalkeeper.myFileProvider"
  14.   
  15.    /** 
  16.     * 是否需要適配7.0權(quán)限 
  17.     * 
  18.     * @return 
  19.     */ 
  20.    public static boolean isNeedAdapt() { 
  21. //24以上版本 
  22.       return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N; 
  23.    } 
  24.   
  25.    public static Uri adaptUriAndGrantPermission(Context context, Intent intent, File file) { 
  26.       Uri uri = adaptUri(context, file); 
  27.       if (uri == null) { 
  28.          return null
  29.       } 
  30.       grantUriPermission(context, intent, uri); 
  31.       return uri; 
  32.    } 
  33.   
  34.    public static Uri adaptUri(Context context, File file) { 
  35.       if (context == null || file == null) { 
  36.          return null
  37.       } 
  38. //網(wǎng)絡(luò)路徑的特殊處理,不需要7.0適配,但必須用parse()方法 
  39.       if (file.getPath().startsWith("http")) { 
  40.          return Uri.parse(file.getPath()); 
  41.       } 
  42.       Uri uri = null
  43.       try { 
  44.          if (isNeedAdapt()) { 
  45. //需要7.0特殊適配 
  46. //通過(guò)系統(tǒng)提供的FileProvider類創(chuàng)建一個(gè)content類型的Uri對(duì)象 
  47.             uri = FileProvider.getUriForFile(context, AUTHORITIES, file); 
  48.          } else { 
  49. //不需要適配 
  50.             uri = Uri.fromFile(file); 
  51.          } 
  52.       } catch (Exception e) { 
  53.          Log.e(TAG, "authorities value error, so can't convert uri !"); 
  54.          e.printStackTrace(); 
  55.       } 
  56.       return uri; 
  57.    } 
  58.   
  59.    /** 
  60.     * 對(duì)第三方應(yīng)用賦予對(duì)uri讀寫(xiě)的權(quán)限 
  61.     * 
  62.     * @param context 
  63.     * @param intent 
  64.     * @param saveUri 適配后的uri 
  65.     */ 
  66.    public static void grantUriPermission(Context context, Intent intent, Uri saveUri) { 
  67.       if (!isNeedAdapt()) { 
  68.          return
  69.       } 
  70.       if (context == null || intent == null || saveUri == null) { 
  71.          return
  72.       } 
  73. //網(wǎng)絡(luò)路徑的特殊處理,不需要權(quán)限 
  74.       if (saveUri.getScheme() != null && saveUri.getScheme().startsWith("http")) { 
  75. //不需要授權(quán) 
  76.          return
  77.       } 
  78. //1、授權(quán)(系統(tǒng)相冊(cè)、相機(jī)、裁剪時(shí)需要)  -- 這種寫(xiě)法待分析 
  79.       List resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); 
  80.       for (ResolveInfo resolveInfo : resInfoList) { 
  81.          String packageName = resolveInfo.activityInfo.packageName; 
  82.          if (TextUtils.isEmpty(packageName)) { 
  83.             continue
  84.          } 
  85.          context.grantUriPermission(packageName, saveUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
  86.       } 
  87. //2、授權(quán)(安裝apk時(shí)需要) 
  88.       intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
  89.    } 
  90.   
  91.    public static void revokeUriPermission(Context context, Intent intent, Uri saveUri) { 
  92.       if (!isNeedAdapt()) { 
  93.          return
  94.       } 
  95.       if (context == null || intent == null || saveUri == null) { 
  96.          return
  97.       } 
  98. //網(wǎng)絡(luò)路徑的特殊處理,不需要權(quán)限 
  99.       if (saveUri.getScheme() != null && saveUri.getScheme().startsWith("http")) { 
  100. //不需要授權(quán) 
  101.          return
  102.       } 
  103.       try { 
  104.          context.revokeUriPermission(saveUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
  105.       } catch (Exception e) { 
  106.          e.printStackTrace(); 
  107.       } 
  108.    } 
  109.  

核心代碼:

  1. uri = FileProvider.getUriForFile(context, AUTHORITIES, file); 

file就是你想要暴露給其他應(yīng)用的文件地址,比喻你要拍照,把結(jié)果保存到file文件中。

AUTHORITIES就是上面第一步中,android:authorities=”${applicationId}.myFileProvider”的實(shí)際值, ${applicationId}取得是app/build.gradle中defaultConfig標(biāo)簽的applicationid值。通過(guò)系統(tǒng)提供的FileProvider類的靜態(tài)方法轉(zhuǎn)換file地址為一個(gè)以content://開(kāi)頭的特殊的uri。如果不轉(zhuǎn)換的話,直接用Uri.fromFile(file),你得到的是一個(gè)file:///xxxxx這樣的uri。就這差別。

轉(zhuǎn)換了uri之后,還需要授權(quán):

  1. /** 
  2.     * 對(duì)第三方應(yīng)用賦予對(duì)uri讀寫(xiě)的權(quán)限 
  3.     * 
  4.     * @param context 
  5.     * @param intent 
  6.     * @param saveUri 適配后的uri 
  7.     */ 
  8.    public static void grantUriPermission(Context context, Intent intent, Uri saveUri) { 
  9.       if (!isNeedAdapt()) { 
  10.          return
  11.       } 
  12.       if (context == null || intent == null || saveUri == null) { 
  13.          return
  14.       } 
  15. //網(wǎng)絡(luò)路徑的特殊處理,不需要權(quán)限 
  16.       if (saveUri.getScheme() != null && saveUri.getScheme().startsWith("http")) { 
  17. //不需要授權(quán) 
  18.          return
  19.       } 
  20. //1、授權(quán)(系統(tǒng)相冊(cè)、相機(jī)、裁剪時(shí)需要)  -- 這種寫(xiě)法待分析 
  21.       List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); 
  22.       for (ResolveInfo resolveInfo : resInfoList) { 
  23.          String packageName = resolveInfo.activityInfo.packageName; 
  24.          if (TextUtils.isEmpty(packageName)) { 
  25.             continue
  26.          } 
  27.          context.grantUriPermission(packageName, saveUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
  28.       } 
  29. //2、授權(quán)(安裝apk時(shí)需要) 
  30.       intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
  31.    }  

里面有兩部分授權(quán)方式,經(jīng)過(guò)多輪測(cè)試,發(fā)現(xiàn)需要同時(shí)使用比較好。

第一種方式,for循環(huán),是因?yàn)橛袝r(shí)候你并不確定需要分享的應(yīng)用的包名是哪一個(gè),所以找到所有有可能的第三方應(yīng)用,全部授權(quán)了。

后來(lái)測(cè)試發(fā)現(xiàn),安裝apk的時(shí)候,只有上面的授權(quán)是不行的,還得加上intent.addFlag的方式再次授權(quán)才行。

最后,需要注意的是:

1、注意7.0的版本判斷。

2、切記,7.0的權(quán)限有可能需要對(duì)sd卡讀寫(xiě),需要6.0的讀寫(xiě)sd卡權(quán)限。當(dāng)你測(cè)試7.0權(quán)限不成功時(shí),考慮下6.0的權(quán)限是否到位了。

其他的沒(méi)的說(shuō)了。就這些。 

責(zé)任編輯:龐桂玉 來(lái)源: Android開(kāi)發(fā)中文站
相關(guān)推薦

2010-03-03 17:02:08

Android手機(jī)

2010-02-04 17:16:30

2011-04-14 09:28:56

IIS 7.0

2016-08-29 20:46:09

Android 7.0牛軋?zhí)?/a>Android 7.0

2020-11-02 00:17:52

vSphere 7.0Kubernetes容器管理

2012-10-09 10:26:52

Linux目錄權(quán)限

2010-05-18 15:54:25

IIS 7.0

2019-11-25 08:00:00

微軟PowerShellPowerShell

2022-06-16 10:38:24

URL權(quán)限源代碼

2015-11-16 10:34:19

Linux動(dòng)態(tài)庫(kù)總結(jié)

2010-02-02 09:08:39

Python 特性

2011-03-10 15:34:21

網(wǎng)絡(luò)管理

2010-03-24 18:59:29

2010-05-27 12:58:16

MySQL性能測(cè)試

2024-01-08 09:43:20

2016-08-29 14:18:48

Testin

2010-05-13 18:01:36

IIS服務(wù)器

2017-01-03 15:46:13

AndroidAndroid7.0

2010-02-23 16:32:14

Python編程

2009-11-11 14:27:32

ADO.NET函數(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)