我们都知道下面两种方法都可以打开Android本地图库:
Intent.ACTION_GET_CONTENT
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_CODE_DOCUMENT);
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_CODE_ALBUM);
但是他们之间的区别是什么呢?
Intent.ACTION_GET_CONTENT 获取的是所有本地图片, Intent.ACTION_PICK 获取的是相册中的图片。
Intent.ACTION_PICK 返回的uri格式只有一种:比如 uri=content://media/external/images/media/34; 而Intent.ACTION_GET_CONTENT 返回的uri格式: 在Android版本4.4以下同Intent.ACTION_PICK 返回的一 样, Android版本4.4以上则返回多种格式:如 uri=content://com.android.providers.media.documents/document/image%3A245743, uri=file:///storage/emulated/0/temp_photo.jpg, uri=content://media/external/images/media/193968 … Intent.ACTION_GET_CONTENT 必须设置setType("image/*") 表示返回的数据类型,否则会报 android.content.ActivityNotFoundException 异常。
下面就Intent.ACTION_GET_CONTENT返回的多种格式根据不同版本来处理:
/**
* 根据Uri获取图片路径,专为Android4.4设计
* @param act
* @param uri
* @return
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getPathFromUriOnKitKat(Activity act, Uri uri) {
/**
* uri=content://com.android.providers.media.documents/document/image%3A293502 4.4以后
* uri=file:///storage/emulated/0/temp_photo.jpg
* uri=content://media/external/images/media/193968
*
* uri=content://media/external/images/media/13 4.4以前
*/
String path = null;
if (DocumentsContract.isDocumentUri(act, uri)) {
// 如果是document类型的Uri,则通过document id处理
String docId = DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
String id = docId.split(":")[1]; // 解析出数字格式的id
String selection = MediaStore.Images.Media._ID + "=" + id;
path = getPathFromUri(act, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
path = getPathFromUri(act, contentUri, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
// 如果是content类型的Uri,则使用普通方式处理
path = getPathFromUri(act, uri, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
// 如果是file类型的Uri,直接获取图片路径即可
path = uri.getPath();
}
return path;
}
/**
* 根据Uri获取图片路径,Android4.4以前
* @param act
* @param uri
* @return
*/
public static String getPathFromUriBeforeKitKat(Activity act, Uri uri) {
return getPathFromUri(act, uri, null);
}
/**
* 通过Uri和selection来获取真实的图片路径
* @param act
* @param uri
* @param selection
* @return
*/
private static String getPathFromUri(Activity act, Uri uri, String selection) {
String path = null;
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = act.getContentResolver().query(uri, projection, selection,null,null);
if(cursor != null){
if(cursor.moveToFirst()){
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
return path;
}
Intent.ACTION_GET_CONTENT不仅仅可以获取本地的图片,还可以获取本地的音频,视频等,同样Intent.ACTION_PICK不仅仅获取图片,还可以获取本地联系人等
(一)、调用本地联系人:
Intent intent=newIntent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent,PICK_CONTACT);
(二)、调用图库,获取所有本地图片:
Intent imageIntent=newIntent(Intent.ACTION_GET_CONTENT);
imageIntent.setType("image/*");
startActivityForResult(imageIntent,PICK_IMAGE);
(三)、调用音乐,获取所有本地音乐文件:
Intent audioIntent=newIntent(Intent.ACTION_GET_CONTENT);
audioIntent.setType("audio/*");
startActivityForResult(audioIntent,PICK_AUDIO);
(四)、调用图库,获取所有本地视频文件:
Intent videoIntent=newIntent(Intent.ACTION_GET_CONTENT);
videoIntent.setType("video/*");
startActivityForResult(videoIntent,PICK_VIDEO);
|