我是靠谱客的博主 彩色秋天,这篇文章主要介绍android4.4及以下选择图片,现在分享给大家,希望可以做个参考。

在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的界面看起来很不错的,如果只求功能第二种就简便一些了

最后

以上就是彩色秋天最近收集整理的关于android4.4及以下选择图片的全部内容,更多相关android4内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(117)

评论列表共有 0 条评论

立即
投稿
返回
顶部