最近开发中领导要求加入图片消息的处理,用了大约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