Android中的Uri和ContentProvider

网络的URL,  URI  ,URN

URI = Universal Resource Identifier 统一资源标志符
URL = Universal Resource Locator 统一资源定位符
URN = Universal Resource Name 统一资源名称

URL代表资源的路径地址,URI代表资源的名称。

URI有两种形式,URL 和 URN

URN:

唯一标识一个实体的标识符,但是不能给出实体的位置。系统可以先在本地寻找一个实体,在它试着在Web上找到该实体之前。它也允许Web位置改变,然而这个实体却还是能够被找到。

标识持久性 Internet 资源。URN 可以提供一种机制,用于查找和检索定义特定命名空间的架构文件。尽管普通的 URL 可以提供类似的功能,但是在这方面,URN 更加强大并且更容易管理,因为 URN 可以引用多个 URL

URN是作为特定内容的唯一名称使用的,与当前资源的所在地无关。使用URN,就可以将资源四处迁移,而不用担心迁移后无法访问。

URN 和 URL 都属于 URI。

URN在web中主要应用是下拉菜单的制作。使用URN时下拉菜单的易扩展性将会得到很大的提高。

P2P【peer-to-peer对等网络,网络的参与者共享他们所拥有的一部分硬件资源(处理能力、存储能力、网络连接能力、打印机等),这些共享资源通过网络提供服务和内容,能被其它对等节点(Peer)直接访问而无需经过中间实体。在此网络中的参与者既是资源、服务和内容的提供者(Server),又是资源、服务和内容的获取者(Client)】下载中使用的磁力链接是URN的一种实现,它可以持久化的标识一个BT资源,资源分布式的存储在P2P网络中,无需中心服务器用户即可找到并下载它。

 下面是例子:

我们一起来看下面这个虚构的例子。这是一个URI

http://bitpoetry.io/posts/hello.html#intro

http://  是定义如何访问资源的方式


bitpoetry.io/posts/hello.html是资源存放的位置

#intro 是资源。


URL是URI的一个子集,告诉我们访问网络位置的方式。如下所示:http://bitpoetry.io/posts/hello.html


URN是URI的子集,包括名字(给定的命名空间内),但是不包括访问方式,如下所示:bitpoetry.io/posts/hello.html#intro 

android的URI与Uri

URI位置在java.net.URI,

Uri位置在android.net.Uri

Uri是Android开发的,扩展了JAVA中URI的一些功能来特定的适用于Android开发,所以大家在开发时,只使用Android 提供的Uri即可。

举例如下:

java.net的标准形式:

**[scheme:][//host:port][path][?query][#fragment] **

**[URI方案:][//主机号:端口号][资源访问路径][?查询条件][#资源] **

http://localhost:8080/user/info?age=20&name=someone#fragment

Android中的Uri

Uri代表要操作的数据,Android上可用的每种资源 - 图像、视频片段等都可以用Uri来表示。Uri唯一标识每种资源。

Uri一般由三部分组成: 
1访问资源的命名机制。 
2存放资源的主机名。 
3资源自身的名称,由路径表示。

android的Uri由以下三部分组成: “content://”、数据的路径、标示ID(可选)

举些例子,如: 
所有联系人的Uri: content://contacts/people 
某个联系人的Uri: content://contacts/people/5 
所有图片Uri: content://media/external 
某个图片的Uri:content://media/external/images/media/4

二、关于多媒体的一些URI: 
存储在sd卡上的音频文件: 
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI 
存储在手机内部存储器上的音频文件: 
MediaStore.Audio.Media.INTERNAL_CONTENT_URI 
SD卡上的图片文件内容: 
MediaStore.Images.Media.EXTERNAL_CONTENT_URI 
手机内部存储器上的图片: 
MediaStore.Images.Media.INTERNAL_CONTENT_URI 
SD卡上的视频: 
MediaStore.Video.Media.EXTERNAL_CONTENT_URI 
手机内部存储器上的视频: 
MediaStore.Video.Media.INTERNAL_CONTENT_URI 
(注:图片的显示名栏:Media.DISPLAY_NAME, 
图片的详细描述栏为:Media.DESCRIPTION 
图片的保存位置:Media.DATA

下面列举一些Android系统中常用的Uri例子:
显示网页:

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); 


路径规划:

Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat startLng&daddr=endLat endLng&hl=en"); 
Intent it = new Intent(Intent.ACTION_VIEW, URI); 
startActivity(it); 


调用拨号程序:

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); 
 
<uses-permission id="Android.permission.CALL_PHONE" /> 


调用发送短信/彩信的程序:

Intent it = new Intent(Intent.ACTION_VIEW); 
it.putExtra("sms_body", "The SMS text"); 
it.setType("vnd.android-dir/mms-sms"); 
startActivity(it);   


发送短信:

Uri uri = Uri.parse("smsto:0800000123"); 
Intent it = new Intent(Intent.ACTION_SENDTO, uri); 
it.putExtra("sms_body", "The SMS text"); 
startActivity(it);  

 
发送彩信:

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); 


发送EMail:

Uri uri = Uri.parse("mailto:[email protected]"); 
Intent it = new Intent(Intent.ACTION_SENDTO, uri); 
startActivity(it); 
 
Intent it = new Intent(Intent.ACTION_SEND); 
it.putExtra(Intent.EXTRA_EMAIL, "[email protected]"); 
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={"[email protected]"};   
String[] ccs={"[email protected]"};   
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 APK

Uri uri = Uri.fromParts("package", strPackageName, null); 
Intent it = new Intent(Intent.ACTION_DELETE, uri); 
startActivity(it); 


调用相册:

public static final String MIME_TYPE_IMAGE_JPEG = "image 
Uri packageURI = Uri.parse("package:"+wistatmap);   
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);   
startActivity(uninstallIntent); 


Install APK:

Uri installUri = Uri.fromParts("package", "xxx", null); 
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri); 
play audio 
Uri playUri = Uri.parse("[url=]file:///sdcard/download/everything.mp3[/url]"); 
returnIt = new Intent(Intent.ACTION_VIEW, playUri); 


搜索应用:

Uri uri = Uri.parse("market://search?q=pname:pkg_name");   
Intent it = new Intent(Intent.ACTION_VIEW, uri);   
startActivity(it);   
//where pkg_name is the full package path for an application 


进入联系人页面:

Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_VIEW); 
intent.setData(People.CONTENT_URI); 
startActivity(intent); 


查看制定联系人:

Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, info.id);//info.id联系人ID 
Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_VIEW); 
intent.setData(personUri); 
startActivity(intent); 


android中Uri:

示æå¾

schema为content:这是android规定的,声明这是android的数据源 

Android的四大组件之一ContentProvider请参考这篇文章https://blog.csdn.net/carson_ho/article/details/76101093

示æå¾

android中自定义的Uri表示了数据源,通过 ContentProvider可以在不同的应用间共享数据源,在Androidmanifest中配置

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

      
    </application>

猜你喜欢

转载自blog.csdn.net/qq_41063141/article/details/86555831