国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android系統(tǒng)的問題分析筆記(9) - Android 中的 Uri 如何使用呢 ?

這篇具有很好參考價(jià)值的文章主要介紹了Android系統(tǒng)的問題分析筆記(9) - Android 中的 Uri 如何使用呢 ?。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

問題

Android 中常用的 uri 如何使用呢 ?(此篇分析基礎(chǔ)為Android 7.1.1系統(tǒng)源碼),參看Android官方說明:https://developer.android.com/reference/android/net/Uri,代碼可在此查看:https://github.com/LineageOS/android_frameworks_base/blob/lineage-20.0/core/java/android/net/Uri.java

1 概述

??通用資源標(biāo)志符 URI (Universal Resource Identifier),URI 在 java.net.URI 中定義,顯然是 Java 提供的一個(gè)類。而 Uri 位置在 android.net.Uri 中定義(Android 源碼內(nèi)位置:frameworks/base/core/java/android/net/Uri.java),是特有針對(duì) Android 系統(tǒng)定義的類。Uri 代表要操作的數(shù)據(jù),Android 上可用的每種資源(圖像、視頻片段、網(wǎng)頁等)都可以用 Uri 來表示。

Uri 的組成部分:

  • 訪問資源的命名機(jī)制(scheme)
  • 存放資源的主機(jī)名(authority)
  • 資源自身的名稱,由路徑表示(path)

Uri 的結(jié)構(gòu):

# 基本形式
[scheme:]scheme-specific-part[#fragment]
# 第二形式
[scheme:][//authority][path][?query][#fragment]
# 第三形式
[scheme:][//host:port][path][?query][#fragment]

特別針對(duì)scheme說明下,Uri中有這么幾種常用標(biāo)識(shí):

  • content : 主要操作的是ContentProvider,它代表的是數(shù)據(jù)庫中的某個(gè)資源
  • http : 某網(wǎng)站資源
  • file : 本地機(jī)器上的資源
  • git : git倉庫中的資源
  • ftp : ftp服務(wù)器上的資源

2 Uri 的結(jié)構(gòu)

2.1 基本形式

# 基本形式
[scheme:]scheme-specific-part[#fragment]

2.2 第二形式

# 第二形式
[scheme:][//authority][path][?query][#fragment]

注意其中規(guī)則:

  1. path 可以有多個(gè),每個(gè)用 / 連接,比如:
    scheme://authority/path1/path2/path3?query#fragment
    
  2. query 參數(shù)可以帶有對(duì)應(yīng)的值,也可以不帶,如果帶對(duì)應(yīng)的值用 = 表示,如:
    //這里有一個(gè)參數(shù)id,它的值是1
    scheme://authority/path1/path2/path3?id=1#fragment
    
  3. query 參數(shù)可以有多個(gè),每個(gè)用 & 連接,如:
    /*
    	這里有3個(gè)參數(shù):
    	參數(shù)1:id,其值是:1
    	參數(shù)2:name,其值是:pedro
    	參數(shù)3:old,沒有對(duì)它賦值,所以它的值是null
    */
    scheme://authority/path1/path2/path3?id = 1&name=pedro&old#fragment
    

??在android中,除了scheme、authority是必須要有的,其它的幾個(gè)path、query、fragment,它們每一個(gè)可以選擇性的要或不要,但順序不能變

2.3 第三形式

第二形式中 authority 又可以分為 host:port 的形式,這是劃分最細(xì)的形式:

# 第三形式
[scheme:][//host:port][path][?query][#fragment]

2.4 例子

可以通過這個(gè)例子檢測下學(xué)習(xí)效果:

http://www.pedro11.com:8080/yourpath/fileName.html?stove=10&path=32&id=4#harvic

3 Uri 的UML類圖

??我把 Android 源碼中 frameworks/base/core/java/android/net/Uri.java (也可在此查看:https://github.com/LineageOS/android_frameworks_base/blob/lineage-20.0/core/java/android/net/Uri.java)導(dǎo)入到 IDEA 工程中,通過 IDEA 的 Diagram 工具生成了它的 UML 類圖(注意其中紫色方法為抽象方法),更方便分析這個(gè)有意思的類(在Uri.java中可以看出它有很多內(nèi)部類,而且內(nèi)部類又繼承了Uri類本身)。
Android系統(tǒng)的問題分析筆記(9) - Android 中的 Uri 如何使用呢 ?
類圖中各個(gè)小圖標(biāo)的含義可查看這里:https://jetbrains.design/intellij/resources/icons_list/

4 從 Uri 中提取 String

  • getScheme() : 獲取Uri中的scheme字符串部分,即 http
  • getSchemeSpecificPart() : 獲取Uri中的scheme-specific-part:部分,即 //www.pedro11.com:8080/yourpath/fileName.html
  • getFragment() : 獲取Uri中的Fragment部分,即 harvic
  • getAuthority() : 獲取Uri中Authority部分,即 www.pedro11.com:8080
  • getPath() : 獲取Uri中path部分,即 /yourpath/fileName.html
  • getQuery() : 獲取Uri中的query部分,即 stove=10&path=32&id=4
  • getHost() : 獲取Authority中的Host字符串,即 www.pedro11.com
  • getPost() : 獲取Authority中的Port字符串,即 8080
  • getPathSegments() : 依次提取出Path的各個(gè)部分的字符串,存入List< String>
  • getQueryParameter(String key) : 通過傳進(jìn)去query中某個(gè)Key的字符串,返回它對(duì)應(yīng)的值
String mUriStr = "http://www.pedro11.com:8080/yourpath/fileName.html?stove=10&path=32&id=4#harvic";
Uri mUri = Uri.parse(mUriStr);
List<String> pathSegList = mUri.getPathSegments();
for (String pathItem:pathSegList){
    Log.d("qijian","pathSegItem:"+pathItem);
}

Log.d(tag,"getQueryParameter(\"stove\"):"+mUri.getQueryParameter("stove"));
Log.d(tag,"getQueryParameter(\"id\"):"+mUri.getQueryParameter("id"));

6 Uri操作工具類

6.1 ContentUris 處理 Uri

ContentUris 有兩個(gè)作用:

  • 為路徑加上ID
  • 從Uri路徑中獲取ID
  1. 為Uri路徑加上ID: withAppendedId(uri, id)
    //比如有這樣一個(gè)Uri
    Uri uri = Uri.parse("content://com.example.yy/book");
    
    //通過ContentUris的withAppendedId()方法,為該Uri加上ID  
    Uri resultUri = ContentUris.withAppendedId(uri, 10);
    
    //最后resultUri為: 
    //content://com.example.yy/book/10
    
  2. 從Uri路徑中獲取ID—parseId(uri)
    Uri uri = Uri.parse("content://com.example.yy/book/10")  
    long bookId= ContentUris.parseId(uri); 
    

6.2 UriMatcher 處理 Uri

待添加文章來源地址http://www.zghlxwxcb.cn/news/detail-488896.html

6.3 uri與file、path相互轉(zhuǎn)化

  1. uri 轉(zhuǎn) file
    file = new File(new URI(uri.toString()));  
    
  2. file 轉(zhuǎn) uri
    URI uri = file.toURI();  
    
  3. uri 轉(zhuǎn) path
    private String getPath(Context context, Uri uri) {  
        String path = null;
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        if (cursor == null) {
            return null;
        }
        if (cursor.moveToFirst()) {
            try {
                path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        cursor.close();
        return path;
     }
    
  4. path 轉(zhuǎn) uri
    Uri uri = Uri.parse(path);  
    
  5. file 轉(zhuǎn) path
    String path = file.getPath();  
    
  6. path 轉(zhuǎn) file
    File file = new File(path);
    

8 常用Uri

//顯示網(wǎng)頁: 
	Uri uri = Uri.parse("http://www.google.com"); 
	Intent it = new Intent(Intent.ACTION_VIEW,uri); 
	startActivity(it); 
 
//顯示地圖: 
	Uri uri = Uri.parse("geo:38.899533,-77.036476"); 
	Intent it = new Intent(Intent.Action_VIEW,uri); 
	startActivity(it); 
 
//路徑規(guī)劃: 
	Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en"); 
	Intent it = new Intent(Intent.ACTION_VIEW,URI); 
	startActivity(it); 
 
//調(diào)用撥號(hào)程序,要使用這個(gè)必須在配置文件中加入<uses-permission id="Android.permission.CALL_PHONE" /> 
	Uri uri = Uri.parse("tel:xxxxxx"); 
	Intent it = new Intent(Intent.ACTION_DIAL, uri);   
	startActivity(it);

	Uri uri = Uri.parse("tel.xxxxxx"); 
	Intent it =new Intent(Intent.ACTION_CALL,uri); 
 
//調(diào)用發(fā)送短信的程序 
	Intent it = new Intent(Intent.ACTION_VIEW); 
	it.putExtra("sms_body", "The SMS text"); 
	it.setType("vnd.android-dir/mms-sms"); 
	startActivity(it);

//發(fā)送短信 
	Uri uri = Uri.parse("smsto:0800000123"); 
	Intent it = new Intent(Intent.ACTION_SENDTO, uri); 
	it.putExtra("sms_body", "The SMS text"); 
	startActivity(it);   

//發(fā)送彩信 
	Uri uri = Uri.parse("content://media/external/images/media/23"); 
	Intent it = new Intent(Intent.ACTION_SEND); 
	it.putExtra("sms_body", "some text"); 
	it.putExtra(Intent.EXTRA_STREAM, uri); 
	it.setType("image/png"); 
	startActivity(it); 
 
//發(fā)送Email 
	Uri uri = Uri.parse("mailto:xxx@abc.com"); 
	Intent it = new Intent(Intent.ACTION_SENDTO, uri); 
	startActivity(it); 
  
	Intent it = new Intent(Intent.ACTION_SEND); 
	it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com"); 
	it.putExtra(Intent.EXTRA_TEXT, "The email body text"); 
	it.setType("text/plain"); 
	startActivity(Intent.createChooser(it, "Choose Email Client"));
  
	Intent it=new Intent(Intent.ACTION_SEND);   
	String[] tos={"me@abc.com"};   
	String[] ccs={"you@abc.com"};   
	it.putExtra(Intent.EXTRA_EMAIL, tos);   
	it.putExtra(Intent.EXTRA_CC, ccs);   
	it.putExtra(Intent.EXTRA_TEXT, "The email body text");   
	it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");   
	it.setType("message/rfc822");   
	startActivity(Intent.createChooser(it, "Choose Email Client")); 
 
//添加附件 
	Intent it = new Intent(Intent.ACTION_SEND);
	it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
	it.putExtra(Intent.EXTRA_STREAM, "[url=]file:///sdcard/mysong.mp3[/url]");
	sendIntent.setType("audio/mp3");
	startActivity(Intent.createChooser(it, "Choose Email Client"));
 
//播放多媒體 
	Intent it = new Intent(Intent.ACTION_VIEW); 
	Uri uri = Uri.parse("[url=]file:///sdcard/song.mp3[/url]"); 
	it.setDataAndType(uri, "audio/mp3"); 
	startActivity(it); 
  
	Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); 
	Intent it = new Intent(Intent.ACTION_VIEW, uri); 
	startActivity(it);   
 
//Uninstall APP
	Uri uri = Uri.fromParts("package", strPackageName, null); 
	Intent it = new Intent(Intent.ACTION_DELETE, uri); 
	startActivity(it); 
 
//調(diào)用相冊(cè) 
	public static final String MIME_TYPE_IMAGE_JPEG = "image/*"; 
	public static final int ACTIVITY_GET_IMAGE = 0; 
	Intent getImage = new Intent(Intent.ACTION_GET_CONTENT); 
	getImage.addCategory(Intent.CATEGORY_OPENABLE); 
	getImage.setType(MIME_TYPE_IMAGE_JPEG); 
	startActivityForResult(getImage, ACTIVITY_GET_IMAGE); 
 
//調(diào)用系統(tǒng)相機(jī)應(yīng)用程序,并存儲(chǔ)拍下來的照片 
	Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
	time = Calendar.getInstance().getTimeInMillis(); 
	intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tucue", time + ".jpg"))); 
	startActivityForResult(intent, ACTIVITY_GET_CAMERA_IMAGE); 
 
//play audio 
	Uri playUri = Uri.parse("[url=]file:///sdcard/download/everything.mp3[/url]"); 
	returnIt = new Intent(Intent.ACTION_VIEW, playUri); 
 
//發(fā)送附件 
	Intent it = new Intent(Intent.ACTION_SEND);   
	it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");   
	it.putExtra(Intent.EXTRA_STREAM, "[url=]file:///sdcard/eoe.mp3[/url]");   
	sendIntent.setType("audio/mp3");   
	startActivity(Intent.createChooser(it, "Choose Email Client")); 
 
//搜索應(yīng)用 
	Uri uri = Uri.parse("market://search?q=pname:pkg_name");   
	Intent it = new Intent(Intent.ACTION_VIEW, uri);   
	startActivity(it);   
 
//進(jìn)入聯(lián)系人頁面 
	Intent intent = new Intent(); 
	intent.setAction(Intent.ACTION_VIEW); 
	intent.setData(People.CONTENT_URI); 
	startActivity(intent); 
 
//查看指定聯(lián)系人 
	Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, info.id);//info.id聯(lián)系人ID 
	Intent intent = new Intent(); 
	intent.setAction(Intent.ACTION_VIEW); 
	intent.setData(personUri);
	startActivity(intent); 

到了這里,關(guān)于Android系統(tǒng)的問題分析筆記(9) - Android 中的 Uri 如何使用呢 ?的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Android進(jìn)階寶典 —如何通過ANR日志分析問題原因

    Android進(jìn)階寶典 —如何通過ANR日志分析問題原因

    當(dāng)系統(tǒng)發(fā)生ANR時(shí),會(huì)主動(dòng)dump trace日志并保存在data/anr/trace.txt文件夾下,我們?cè)谀玫絘nr日志之后,就可以著手分析日志;或者可以通過bugreport命令來拉取日志,具體命令如下: 當(dāng)然我們?cè)诮庾x日志的時(shí)候,肯定是需要一些去查詢,判斷到底是哪種類型的問題導(dǎo)致了ANR。

    2024年02月05日
    瀏覽(24)
  • Android問題筆記四十三:JNI 開發(fā)如何快速定位崩潰問題

    Android問題筆記四十三:JNI 開發(fā)如何快速定位崩潰問題

    Unity3D特效百例 案例項(xiàng)目實(shí)戰(zhàn)源碼 Android-Unity實(shí)戰(zhàn)問題匯總 游戲腳本-輔助自動(dòng)化 Android控件全解手冊(cè) 再戰(zhàn)Android系列 Scratch編程案例 軟考全系列 Unity3D學(xué)習(xí)專欄 藍(lán)橋系列 ChatGPT和AIGC 專注于 Android/Unity 和各種游戲開發(fā)技巧,以及 各種資源分享 (網(wǎng)站、工具、素材、源碼、游戲等

    2024年02月05日
    瀏覽(23)
  • Android問題筆記 -如何實(shí)現(xiàn)代碼控制自動(dòng)旋轉(zhuǎn)開關(guān)的變更以及當(dāng)前狀態(tài)

    Android問題筆記 -如何實(shí)現(xiàn)代碼控制自動(dòng)旋轉(zhuǎn)開關(guān)的變更以及當(dāng)前狀態(tài)

    專欄分享 點(diǎn)擊跳轉(zhuǎn)=Unity3D特效百例 點(diǎn)擊跳轉(zhuǎn)=案例項(xiàng)目實(shí)戰(zhàn)源碼 點(diǎn)擊跳轉(zhuǎn)=游戲腳本-輔助自動(dòng)化 點(diǎn)擊跳轉(zhuǎn)=Android控件全解手冊(cè) 點(diǎn)擊跳轉(zhuǎn)=Scratch編程案例 點(diǎn)擊跳轉(zhuǎn)=軟考全系列 眾所周知,人生是一個(gè)漫長的流程,不斷 克服困難 ,不斷反思前進(jìn)的過程。在這個(gè)過程中會(huì)產(chǎn)生很多對(duì)

    2024年02月08日
    瀏覽(25)
  • 如何分析K8S中的OOMKilled問題(Exit Code 137)

    當(dāng) Kubernetes 集群中的容器超過其內(nèi)存限制時(shí),Kubernetes 系統(tǒng)可能會(huì)終止該容器并顯示“OOMKilled”錯(cuò)誤,這表明該進(jìn)程由于內(nèi)存不足而被終止。此錯(cuò)誤的退出代碼是 137。 如果遇到錯(cuò)誤,Pod 的狀態(tài)將顯示“OOMKilled”,您可以使用以下命令查看該錯(cuò)誤: Out-Of-Memory Killer (OOMKiller) 是

    2024年02月03日
    瀏覽(21)
  • opencv android sdk 使用中的問題

    opencv android sdk 使用中的問題

    在build.gradle(:app)中添加以下內(nèi)容 再在工程的build.gradel中添加如下內(nèi)容 版本根據(jù)實(shí)際情況修改. 如下圖 在opencv的build.gradle中添加工程的命名空間即可. 如下圖 opencv build.gradle

    2024年02月10日
    瀏覽(18)
  • android 如何分析應(yīng)用的內(nèi)存(十七)——使用MAT查看Android堆

    android 如何分析應(yīng)用的內(nèi)存(十七)——使用MAT查看Android堆

    前一篇文章,介紹了使用Android profiler中的memory profiler來查看Android的堆情況。 如Android 堆中有哪些對(duì)象,這些對(duì)象的引用情況是什么樣子的。 可是我們依然面臨一個(gè)比較嚴(yán)峻的挑戰(zhàn):不管是app開發(fā)者,還是內(nèi)存分析者而言,堆中的對(duì)象,非常之多,不僅有Android 原生的類,還

    2024年02月13日
    瀏覽(26)
  • Android各個(gè)應(yīng)用商店Uri

    三星: URL: https://apps.samsung.com/appquery/AppRating.as?appId=PackageName URI: “samsungapps://AppRating/PackageName” Onestore ”onestore://common/product/bg_update/PID“ https://m.onestore.co.kr/mobilepoc/apps/appsDetail.omp?prodId=PID Google Play “market://details?id=PackageName” 華為 “appmarket://details?id=PackageName” vivo “vivoMa

    2024年01月22日
    瀏覽(19)
  • android uri轉(zhuǎn)換file

    下載jar包c(diǎn)ommons-io-2.11.0.jar導(dǎo)入項(xiàng)目工程,導(dǎo)入import org.apache.commons.io.FileUtils; 如果使用系統(tǒng)import android.os.FileUtils;有些手機(jī)不兼容會(huì)導(dǎo)致項(xiàng)目崩潰

    2024年02月11日
    瀏覽(14)
  • 論文筆記 - 對(duì)話系統(tǒng)中的 OOD (Out of Domain出域)問題

    論文筆記 - 對(duì)話系統(tǒng)中的 OOD (Out of Domain出域)問題

    徐阿衡 人工智能與機(jī)器學(xué)習(xí)工程師 最近看了下 2021年關(guān)于 OOD 的幾篇 paper,記錄一下~ 對(duì)話系統(tǒng)中的 domain 都是預(yù)先定義好 的,而在實(shí)際應(yīng)用場景中,會(huì)有很多現(xiàn)有系統(tǒng)回答不了的問題( out of the design scope ),我們把系統(tǒng) 支持的意圖稱為? in-domain (IND) ,系統(tǒng)不支持的意圖稱

    2024年02月09日
    瀏覽(28)
  • Android問題筆記 - 使用SDK33導(dǎo)致xml布局代碼沒有任何提示了

    Android問題筆記 - 使用SDK33導(dǎo)致xml布局代碼沒有任何提示了

    專欄分享 點(diǎn)擊跳轉(zhuǎn)=Unity3D特效百例 點(diǎn)擊跳轉(zhuǎn)=案例項(xiàng)目實(shí)戰(zhàn)源碼 點(diǎn)擊跳轉(zhuǎn)=游戲腳本-輔助自動(dòng)化 點(diǎn)擊跳轉(zhuǎn)=Android控件全解手冊(cè) 點(diǎn)擊跳轉(zhuǎn)=Scratch編程案例 點(diǎn)擊跳轉(zhuǎn)=軟考全系列 眾所周知,人生是一個(gè)漫長的流程,不斷 克服困難 ,不斷反思前進(jìn)的過程。在這個(gè)過程中會(huì)產(chǎn)生很多對(duì)

    2024年02月11日
    瀏覽(29)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包