Android获取本地图片之ACTION_GET_CONTENT与ACTION_PICK区别
2017-07-10 14:53
267 查看
我们都知道下面两种方法都可以打开Android本地图库:
uri=content://media/external/images/media/34;
而
在Android版本4.4以下同
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
…
Android4.4之前处理:
Intent.ACTION_GET_CONTENT
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, REQUEST_CODE_DOCUMENT);
Intent.ACTION_PICK
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_ 4000 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返回的多种格式根据不同版本来处理:
Android4.4之后处理:/** * 根据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; }
Android4.4之前处理:
/** * 根据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);
相关文章推荐
- [Android] ACTION_GET_CONTENT与ACTION_PICK的区别
- Android 巧用Itent.ACTION_PICK和Intent.ACTION_GET_CONTENT
- android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
- Android开发笔记——Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
- Android 巧用Itent.ACTION_PICK和Intent.ACTION_GET_CONTENT(三)
- android 获取本地图片路径失败,cursor.getString(column_index);返回null
- 81ACTION_GET_CONTENT和ACTION_PICK的区别
- Intent.ACTION_GET_CONTENT和Intent.ACTION_PICK的使用区别
- android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
- Android 巧用Itent.ACTION_PICK和Intent.ACTION_GET_CONTENT(三)
- Android 4.4以上获取图片URI格式不同导致getContentResolver返回null
- Intent.ACTION_PICK和Intent.ACTION_GET_CONTENT 调用系统联系人;图片;音乐;视频;
- Android 巧用Itent.ACTION_PICK和Intent.ACTION_GET_CONTENT(一)
- android 获取本地图片路径失败,cursor.getString(column_index);返回null
- Android——ACTION_GET_CONTENT和ACTION_PICK
- Android 巧用Itent.ACTION_PICK和Intent.ACTION_GET_CONTENT(二)
- android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
- Android网络获取图片保存到本地,并在ViewPager中显示图片
- android中获取context的多种方法的区别(this,getbascontext(),getApplicationcontext())
- android 获取本地全部图片列表的实现及源码下载(二)