使用范例:图库选择图片打印成pdf图片后使用toast提示文件保存路径
/frameworks/base/packages/DocumentsUI/src/com/android/documentsui/DocumentsActivity.java
函数 private void onFinished(Uri... uris) { 中增加以下代码
if(android.os.SystemProperties.getBoolean("ro.print.showtoast", false)){
Context context = (Context)DocumentsActivity.this;
String fileName = getPath(context, uris[0]);
Log.d("jimbo", "000 fileName=" + fileName);
if(fileName == null){ //onFinished: uri=[content://com.mediatek.hotknotbeam.documents/document/%2Fstorage%2Fsdcard0%2FHotKnot%3A333333333333333.pdf] 打印到hotknot文件中显示不出来,所以才增加以下代码
// MediaStore (and general)
if ("content".equalsIgnoreCase(uris[0].getScheme())) {
fileName = getDataColumn(context, uris[0], null, null);
Log.d("jimbo", "111 fileName=" + fileName);
}
// File
else if ("file".equalsIgnoreCase(uris[0].getScheme())) {
fileName = uris[0].getPath();
Log.d("jimbo", "222 fileName=" + fileName);
}
}
Log.d("jimbo", "end fileName=" + fileName);
// show toast
if(fileName != null){
String toastStr = DocumentsActivity.this.getString(R.string.title_save)+":"+ fileName;
android.widget.Toast.makeText(this, toastStr, android.widget.Toast.LENGTH_LONG).show();
}
}
以下代码是度娘到的代码:
public static String getPath(final Context context, final Uri uri) {
// DocumentProvider
if (DocumentsContract.isDocumentUri(context, uri)) {
//Log.d("jimbo", " 0000000000000000000" );
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
//Log.d("jimbo", " 0000000000111111111" );
if ("primary".equalsIgnoreCase(type)) {
return android.os.Environment.getExternalStorageDirectory() + "/" + split[1];
}
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = android.content.ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
//Log.d("jimbo", " 00000000002222222" );
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
};
//Log.d("jimbo", " 0000000000333333" );
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
//Log.d("jimbo", " 11111111111111111" );
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
//Log.d("jimbo", " 2222222222222" );
return uri.getPath();
}
<span style="white-space:pre"> </span>Log.d("jimbo", " end---------- return null" );
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
打印后的程序log结果:
D/jimbo ( 2630): 000 fileName=null
D/jimbo ( 2630): 111 fileName=/storage/sdcard0/HotKnot/IMG_20150101_085108.jpg (10).pdf
D/jimbo ( 2630): end fileName=/storage/sdcard0/HotKnot/IMG_20150101_085108.jpg (10).pdf
D/jimbo ( 2630): 0000000000000000000
D/jimbo ( 2630): 00000000002222222
D/jimbo ( 2630): 000 fileName=/storage/sdcard0/Download/IMG_20150101_085108.jpg (13).pdf
D/jimbo ( 2630): end fileName=/storage/sdcard0/Download/IMG_20150101_085108.jpg (13).pdf
以下转自:http://blog.csdn.net/sjz4860402/article/details/51223057
URI简介
概念:统一资源标识符(Uniform Resource Identifier)
1.访问资源的命名机制(scheme)
2.存放资源的主机名(authority)
3.资源自身的名称,由路径表示(path)
格式:scheme:// authority//path,其中authority中又包括了host和port两部分。
例如:content://com.example.project:200/folder/subfolder/etc
\---------/ \----------------------------/ \----/ \------------------------/
Scheme host port path
\------------------------------------/
Authority
用处:uri主要用来表示一个资源。这个资源有很多种类,包括图片,视频,文件等。
针对资源的种类,uri用以下几种scheme标识:
1.Content:主要操作的是ContentProvider,所以它代表的是数据库中的某个资源2.http:一个网站资源
3.file:本地机器上的某个资源
4.git:git仓库中某个资源
5.ftp:服务器上的某个资源
6.ed2k:(电驴协议)
7.等等………
以上的资源例子如下:
在Android中,由于很多资源(音频、视频、图片、以及个人通信录信息)都存入了数据库,所以Android中对资源的使用一般是通过ContentProivder访问数据库,见得比较多的就是Content这种类型的uri。所以我们这里也只是着重讲Content类型的uri。
二.uri详解
1.Authority部分:在看Authority之前,让我们先了解一下ContentProvider。
ContentProvider在android中的作用是对外共享数据, 也就是说你可以通过ContentProvider把应用中的数据共享给其他应用访问,其他应用可以通过ContentProvider对你应用中的数据进行添删改查。那么其他的应用该怎么找到这个ContentProvider?答案在AndroidManifest.xml中配置:
在这里,规定了这个TelephonyProvider内容提供者对外提供的主机(authorities)为telephony。
好了,让我们回去看看content://com.example.project:200/folder/subfolder/etc这个例子。host主要是用com.example.project来表示,这根本就是一个包名啊。但是通过AndroidManifest.xml配置后,就看到了最常见的uri:
所有SIM卡信息的Uri: content:// telephony/siminfo
某张SIM卡的Uri: content:// telephony/siminfo/2
所有图片Uri: content://media/external
某个图片的Uri:content://media/external/images/media/4
2.path部分:
path部分就简单了,既然Authority指定了一个ContentProvider。那么如果我们需要某个具体的资源,就需要通过查询数据库表,在表中存储的位置等等信息了。所以content:// telephony/siminfo/2中siminfo代表的是数据库表名,2代表的是表中某个具体的数值。
三.Uri及它的工具类
Uri主要的结构如下:
Uri的内部类:
------- StringUri 子孙类,使用不明
------- OpaqueUri 子孙类,使用不明
------- HierarchicalUri子孙类,Builder构建Uri的实例对象主要是使用了这个类。
------- Builder 静态类,用于获取Uri实例对象。
------- ….
由于Uri的构造方法是私有的,所以它提供了Builder这个静态类用于获取Uri的实例对象。除了获取对象外,它还提供了很多实用的方法,比如:
Public Uri Build()-------获取Uri的实例对象
Public Builder encodedPath(String path)--------根据path构建Builder对象
public Builder authority(String authority)-------根据authority构建Builder对象
URI使用实例:
Uri的总结也就这些,最后,记录一下常用的几个Uri:
显示网页:
1. Uri uri = Uri.parse("http://www.google.com");
2. Intent it = new Intent(Intent.ACTION_VIEW,uri);
3. startActivity(it);
显示地图:
1. Uri uri = Uri.parse("geo:38.899533,-77.036476");
2. Intent it = new Intent(Intent.Action_VIEW,uri);
3. startActivity(it);
路径规划:
1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
2. Intent it = new Intent(Intent.ACTION_VIEW,URI);
3. startActivity(it);
拨打电话:
调用拨号程序
1. Uri uri = Uri.parse("tel:xxxxxx");
2. Intent it = new Intent(Intent.ACTION_DIAL, uri);
3. startActivity(it);
1. Uri uri = Uri.parse("tel.xxxxxx");
2. Intent it =new Intent(Intent.ACTION_CALL,uri);
3. 要使用这个必须在配置文件中加入<uses-permission id="Android.permission.CALL_PHONE" />
发送SMS/MMS
调用发送短信的程序
1. Intent it = new Intent(Intent.ACTION_VIEW);
2. it.putExtra("sms_body", "The SMS text");
3. it.setType("vnd.android-dir/mms-sms");
4. startActivity(it);
发送短信
1. Uri uri = Uri.parse("smsto:0800000123");
2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
3. it.putExtra("sms_body", "The SMS text");
4. startActivity(it);
发送彩信
1. Uri uri = Uri.parse("content://media/external/images/media/23");
2. Intent it = new Intent(Intent.ACTION_SEND);
3. it.putExtra("sms_body", "some text");
4. it.putExtra(Intent.EXTRA_STREAM, uri);
5. it.setType("image/png");
6. startActivity(it);
发送Email
1.
2. Uri uri = Uri.parse("mailto:xxx@abc.com");
3. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
4. startActivity(it);
1. Intent it = new Intent(Intent.ACTION_SEND);
2. it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
3. it.putExtra(Intent.EXTRA_TEXT, "The email body text");
4. it.setType("text/plain");
5. startActivity(Intent.createChooser(it, "Choose Email Client"));
1. Intent it=new Intent(Intent.ACTION_SEND);
2. String[] tos={"me@abc.com"};
3. String[] ccs={"you@abc.com"};
4. it.putExtra(Intent.EXTRA_EMAIL, tos);
5. it.putExtra(Intent.EXTRA_CC, ccs);
6. it.putExtra(Intent.EXTRA_TEXT, "The email body text");
7. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
8. it.setType("message/rfc822");
9. startActivity(Intent.createChooser(it, "Choose Email Client"));
添加附件
1. Intent it = new Intent(Intent.ACTION_SEND);
2. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
3. it.putExtra(Intent.EXTRA_STREAM, "[url=]file:///sdcard/mysong.mp3[/url]");
4. sendIntent.setType("audio/mp3");
5. startActivity(Intent.createChooser(it, "Choose Email Client"));
播放多媒体
1.
2. Intent it = new Intent(Intent.ACTION_VIEW);
3. Uri uri = Uri.parse("[url=]file:///sdcard/song.mp3[/url]");
4. it.setDataAndType(uri, "audio/mp3");
5. startActivity(it);
1. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
3. startActivity(it);
Uninstall 程序
1. Uri uri = Uri.fromParts("package", strPackageName, null);
2. Intent it = new Intent(Intent.ACTION_DELETE, uri);
3. startActivity(it);
//调用相册
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);
//调用系统相机应用程序,并存储拍下来的照片
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);
uninstall apk
/**未测试
Uri uninstallUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);
*/
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);
//发送附件
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"));
//搜索应用
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);