Intent.ACTION_GET_CONTENT和Intent.ACTION_PICK的使用区别
2017-07-08 16:51
375 查看
在android中选择图片的时候,打开相册选择图片(根据是否4.4设置不同action), if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { intent.setAction(Intent.ACTION_OPEN_DOCUMENT); } else { intent.setAction(Intent.ACTION_GET_CONTENT); }; 在onActivityResult里面返回的Uri uri = data.getData();如果是android4.4 uri格式为content://com.android.providers.media.documents/document/image:3952,4.4以下格式为 content://media/external/images/media/3951,要获取图片的存储路径需要: if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT){ String wholeID = DocumentsContract.getDocumentId(contentUri); String id = wholeID.split(:)[1]; String[] column = { MediaStore.Images.Media.DATA }; String sel = MediaStore.Images.Media._ID + =?; Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[] { id }, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); }else{ String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolver().query(contentUri, projection, null, null, null); int column_i 4000 ndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); filePath = cursor.getString(column_index); }。 另一种方式就是直接设置打开相册的action, intent.setAction(Intent.ACTION_PICK); intent.setData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
后面的onActivityResult里面就可以不区分4.4版本就可以获取到路径。
总的来说第一种方式的效果对于4.4的界面看起来很不错的,如果只求功能第二种就简便一些了。
使用过程中的一些体会,如有不当望指正...
相关文章推荐
- Android开发笔记——Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
- Intent.ACRION.view , Intent.Action.pick intent.action.get_content几个的理解
- 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之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
- 81ACTION_GET_CONTENT和ACTION_PICK的区别
- Intent.ACTION_PICK和Intent.ACTION_GET_CONTENT 调用系统联系人;图片;音乐;视频;
- [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妙用
- ACTION_PICK与ACTION_GET_CONTENT
- android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
- Intent.ACTION_GET_CONTENT用法
- Android——ACTION_GET_CONTENT和ACTION_PICK
- jQuery里ajax的post与get使用与区别