Intent.ACTION_GET_CONTENT和Intent.ACTION_PICK的使用区别

 

在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_index = 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的界面看起来很不错的,如果只求功能第二种就简便一些了。
使用过程中的一些体会,如有不当望指正...


 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值