IM中图片消息处理(一)

来源:互联网 发布:股市日历软件 编辑:程序博客网 时间:2024/06/08 14:21

最近开发中领导要求加入图片消息的处理,用了大约2天时间完成了全部的开发任务,剩下的就是优化工作。

鉴于我刚写完,于是我趁热打铁,对开发过程进行了整理,便于日后完善该模块。若有问题,也请大家及时提出,便于完善,共同进步。

从本节开始,我会分几个部分逐一进行整理。

说到发送图片,我们首先肯定是要选择图片,直接上代码:

Intent intent = new Intent();intent.addCategory(Intent.CATEGORY_OPENABLE);intent.setAction(Intent.ACTION_GET_CONTENT);intent.setType("image/*");((Activity) getContext()).startActivityForResult(intent, IMFragment.REQUEST_CODE_SEND_IMAGE);

Intent里面有一个“ACTION_GET_CONTENT”字符串常量,该常量让用户选择特定类型的数据,并返回该数据的URI.
我们利用该常量,然后设置类型为“image/*”,就可获得Android手机内的所有image。

API中是这样说的:

ACTION_GET_CONTENT with MIME type */* and category CATEGORY_OPENABLE -- Display all pickers for data that can be opened with ContentResolver.openInputStream(), allowing the user to pick one of them and then some data inside of it and returning the resulting URI to the caller. This can be used, for example, in an e-mail application to allow the user to pick some data to include as an attachment.

通过上面的方法,intent中包含了图片的uri.


从本地获取到图片后一定会从图片选择界面返回到初始界面:

@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {    if (resultCode == Activity.RESULT_OK) {        if (requestCode == REQUEST_CODE_SEND_IMAGE) {            Uri uri = data.getData();            if (uri == null) {                return;    }            String imagePath = Utils.getImagePath(getActivity(), uri);            if (!TextUtils.isEmpty(imagePath)) {                if (imagePath.startsWith("file://")) {                    imagePath = imagePath.substring(7);                }                //接下来就执行发送图片            }        }    }    super.onActivityResult(requestCode, resultCode, data);}

我们会从data中获取到刚才选择图片的uri,(uri:content://com.android.providers.media.documents/document/image%3A1732)

之后主要是通过uri获取imagePath,发送主要是用到imagePath.(/storage/emulated/0/sina/weibo/.weibo_pic_newthumb.3924028656085872.jpg)

这里留下了执行发送操作的接口,下节将从发送图片开始。


1 0
原创粉丝点击