本篇文章為大家展示了如何在android7.0中獲取文件的Uri,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網綜合服務,包含不限于成都網站設計、成都網站制作、富錦網絡推廣、微信小程序開發(fā)、富錦網絡營銷、富錦企業(yè)策劃、富錦品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供富錦建站搭建服務,24小時服務熱線:13518219792,官方網址:www.chinadenli.net
//res目錄下創(chuàng)建xml,設置要分享的目錄,名稱定義file_paths <?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="myFile" path ="" /> </paths> // 注冊provider <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.lzp.fileprovider" android:exported="false" // 賦予Uri權限 android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" // 設置要分享的路徑 android:resource="@xml/file_paths"/> </provider>
設置分享的路徑有三種:
<files-path/>代表的根目錄: Context.getFilesDir()
<external-path/>代表的根目錄:Environment.getExternalStorageDirectory()
<cache-path/>代表的根目錄: getCacheDir()
provider注冊完了之后,就需要稍微修改一下我們的java代碼:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 判斷是否是7.0
if(Build.VERSION.SDK_INT >= 24){
// 適配android7.0 ,不能直接訪問原路徑
// 需要對intent 授權
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setDataAndType(FileProvider.getUriForFile(context, context.getPackageName() + ".fileProvider",new File(filePath)),"application/vnd.android.package-archive");
}
else{
i.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive");
}
context.startActivity(i);在java代碼中,我們先判斷了當前的手機的系統(tǒng)版本,如果是7.0以下就使用之前的Uri,如果是7.0以上,開始通過Provider來獲取加密后的Uri。
最終得到的Uri的格式:
content://com.lzp.fileprovider/myFile/xxxxx.apk
看來主要是把分享出去的路徑進行了加密,不能直接看到文件的完整路徑。
上述內容就是如何在android7.0中獲取文件的Uri,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當前標題:如何在android7.0中獲取文件的Uri
轉載注明:http://www.chinadenli.net/article2/igpsic.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供虛擬主機、面包屑導航、App開發(fā)、網站維護、網站營銷、網站改版
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)