Android仿微信发表说说实现拍照、多图上传功能
程序员文章站
2024-03-31 13:03:10
本文实例为大家分享了android仿微信发表说说、心情功能,供大家参考,具体内容如下
既能实现拍照,选图库,多图案上传的案例,目前好多app都有类似微信朋友圈的功能,能过...
本文实例为大家分享了android仿微信发表说说、心情功能,供大家参考,具体内容如下
既能实现拍照,选图库,多图案上传的案例,目前好多app都有类似微信朋友圈的功能,能过发表说说等附带图片上传。下面的就是实现该功能的过程:大家还没有看过android retrofit 2.0框架上传图片解决方案这篇文章,在看今天的就很容易,接在本项目中用到了一个library:photopicker,封装了图片的选择功能,是否选相机,还有选中图片后可以查看图片的功能。
一、 首先:将photopicker到工程中
(1)、先简单讲解一下photopickerintent的用法:
photopickerintent intent = new photopickerintent(mainactivity.this); intent.setselectmodel(selectmodel.multi); // intent.setshowcarema(true); // 是否显示拍照 intent.setmaxtotal(6); // 最多选择照片数量,默认为6 intent.setselectedpaths(imagepaths); // 已选中的照片地址, 用于回显选中状态 startactivityforresult(intent, request_camera_code);
(2)、设置好之后,重写onactivityresult方法处理选中图片和预览加载适配器
@override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if(resultcode == result_ok) { switch (requestcode) { // 选择照片 case request_camera_code: loadadpater(data.getstringarraylistextra(photopickeractivity.extra_result)); break; // 预览 case request_preview_code: loadadpater(data.getstringarraylistextra(photopreviewactivity.extra_result)); break; } } }
二、重点在gridadapter
(1)、定义一个变量 mmaxposition,
(2)、写一个获取最大位置的方法:用来获取item的最大位置。
//重点在这里,当position==mmaxposition-1让"+"选择图片的默认图片显示。 //当目前最多6中图片,则最大7时,图片影藏。 if (position==mmaxposition-1) { holder.image.setimageresource(r.mipmap.ic_launcher); holder.image.setvisibility(view.visible); if(position==6&&mmaxposition==7){ holder.image.setimageresource(r.mipmap.ic_launcher); holder.image.setvisibility(view.gone); } } else { final string path=listurls.get(position); glide.with(mainactivity.this) .load(new file(path)) .placeholder(r.mipmap.default_error) .error(r.mipmap.default_error) .centercrop() .crossfade() .into(holder.image); }
(3)、关于图片的显示:
private class gridadapter extends baseadapter{ private arraylist<string> listurls; private int mmaxposition; private layoutinflater inflater; public gridadapter(arraylist<string> listurls) { this.listurls = listurls; inflater = layoutinflater.from(mainactivity.this); } public int getcount() { mmaxposition = listurls.size()+1; return mmaxposition; } public int getmaxposition(){ return mmaxposition; } @override public string getitem(int position) { return listurls.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; if (convertview == null) { holder = new viewholder(); convertview = inflater.inflate(r.layout.item_image, parent,false); holder.image = (imageview) convertview.findviewbyid(r.id.imageview); convertview.settag(holder); } else { holder = (viewholder)convertview.gettag(); } log.d("", "position:"+position+" mmaxposition:"+mmaxposition); //重点在这里,当position==mmaxposition-1让"+"选择图片的默认图片显示。 //当目前最多6中图片,则最大7时,图片影藏。 if (position==mmaxposition-1) { // holder.image.settag("default"); holder.image.setimageresource(r.mipmap.ic_launcher); holder.image.setvisibility(view.visible); if(position==6&&mmaxposition==7){ holder.image.setimageresource(r.mipmap.ic_launcher); holder.image.setvisibility(view.gone); } } else { final string path=listurls.get(position); glide.with(mainactivity.this) .load(new file(path)) .placeholder(r.mipmap.default_error) .error(r.mipmap.default_error) .centercrop() .crossfade() .into(holder.image); }
三、gridview中item的点击事件
gridview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { if (position == gridadapter.getmaxposition() - 1) { photopickerintent intent = new photopickerintent(mainactivity.this); intent.setselectmodel(selectmodel.multi); intent.setshowcarema(true); // 是否显示拍照 intent.setmaxtotal(6); // 最多选择照片数量,默认为9 intent.setselectedpaths(imagepaths); // 已选中的照片地址, 用于回显选中状态 startactivityforresult(intent, request_camera_code); }else{ photopreviewintent intent = new photopreviewintent(mainactivity.this); intent.setcurrentitem(position); intent.setphotopaths(imagepaths); startactivityforresult(intent, request_preview_code); } } });
整体源码:
package com.lidong.photopickersample; import android.content.intent; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.baseadapter; import android.widget.button; import android.widget.edittext; import android.widget.gridview; import android.widget.imageview; import com.bumptech.glide.glide; import com.lidong.photopicker.imagecapturemanager; import com.lidong.photopicker.photopickeractivity; import com.lidong.photopicker.photopreviewactivity; import com.lidong.photopicker.selectmodel; import com.lidong.photopicker.intent.photopickerintent; import com.lidong.photopicker.intent.photopreviewintent; import org.json.jsonarray; import java.io.file; import java.util.arraylist; /** * @ * @author lidong * @date 2016-02-29 */ public class mainactivity extends appcompatactivity { private static final int request_camera_code = 10; private static final int request_preview_code = 20; private arraylist<string> imagepaths = new arraylist<>(); private imagecapturemanager capturemanager; // 相机拍照处理类 private gridview gridview; private int columnwidth; private gridadapter gridadapter; private button mbutton; private string depp; private edittext textview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); gridview = (gridview) findviewbyid(r.id.gridview); mbutton = (button) findviewbyid(r.id.button); textview= (edittext)findviewbyid(r.id.et_context); int cols = getresources().getdisplaymetrics().widthpixels / getresources().getdisplaymetrics().densitydpi; cols = cols < 3 ? 3 : cols; gridview.setnumcolumns(cols); // preview gridview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { if (position == gridadapter.getmaxposition() - 1) { photopickerintent intent = new photopickerintent(mainactivity.this); intent.setselectmodel(selectmodel.multi); intent.setshowcarema(true); // 是否显示拍照 intent.setmaxtotal(6); // 最多选择照片数量,默认为9 intent.setselectedpaths(imagepaths); // 已选中的照片地址, 用于回显选中状态 startactivityforresult(intent, request_camera_code); }else{ photopreviewintent intent = new photopreviewintent(mainactivity.this); intent.setcurrentitem(position); intent.setphotopaths(imagepaths); startactivityforresult(intent, request_preview_code); } } }); gridadapter = new gridadapter(imagepaths); gridview.setadapter(gridadapter); mbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { depp =textview.gettext().tostring().trim()!=null?textview.gettext().tostring().trim():"woowoeo"; new thread(){ @override public void run() { super.run(); fileuploadmanager.upload(imagepaths,depp); } }.start(); } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if(resultcode == result_ok) { switch (requestcode) { // 选择照片 case request_camera_code: loadadpater(data.getstringarraylistextra(photopickeractivity.extra_result)); break; // 预览 case request_preview_code: loadadpater(data.getstringarraylistextra(photopreviewactivity.extra_result)); break; } } } private void loadadpater(arraylist<string> paths){ if(imagepaths == null){ imagepaths = new arraylist<>(); } imagepaths.clear(); imagepaths.addall(paths); try{ jsonarray obj = new jsonarray(imagepaths); log.e("--", obj.tostring()); }catch (exception e){ e.printstacktrace(); } gridadapter.notifydatasetchanged(); } private class gridadapter extends baseadapter{ private arraylist<string> listurls; private int mmaxposition; private layoutinflater inflater; public gridadapter(arraylist<string> listurls) { this.listurls = listurls; inflater = layoutinflater.from(mainactivity.this); } public int getcount() { if (listurls.size() == 9) { mmaxposition = listurls.size()+1; } else { mmaxposition = listurls.size()+1; } return mmaxposition; } public int getmaxposition(){ return mmaxposition; } @override public string getitem(int position) { return listurls.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; if (convertview == null) { holder = new viewholder(); convertview = inflater.inflate(r.layout.item_image, parent,false); holder.image = (imageview) convertview.findviewbyid(r.id.imageview); convertview.settag(holder); } else { holder = (viewholder)convertview.gettag(); } log.d("", "position:"+position+" mmaxposition:"+mmaxposition); if (position==mmaxposition-1) { // holder.image.settag("default"); holder.image.setimageresource(r.mipmap.ic_launcher); holder.image.setvisibility(view.visible); if(position==6&&mmaxposition==7){ holder.image.setimageresource(r.mipmap.ic_launcher); holder.image.setvisibility(view.gone); } } else { final string path=listurls.get(position); glide.with(mainactivity.this) .load(new file(path)) .placeholder(r.mipmap.default_error) .error(r.mipmap.default_error) .centercrop() .crossfade() .into(holder.image); } return convertview; } public class viewholder { public imageview image; } } }
上传的类:fileuploadmanager.java
package com.lidong.photopickersample; import android.util.log; import com.squareup.okhttp.mediatype; import com.squareup.okhttp.requestbody; import java.io.file; import java.util.arraylist; import retrofit.call; import retrofit.callback; import retrofit.gsonconverterfactory; import retrofit.response; import retrofit.retrofit; import retrofit.http.multipart; import retrofit.http.post; import retrofit.http.part; /** * created by lidong on 2016/2/29 */ public class fileuploadmanager { private static final string endpoint = "http://192.168.1.122:8080"; public interface fileuploadservice { /** * 上传一张图片 * @param description * @param imgs * @return */ @multipart @post("/upload") call<string> uploadimage(@part("filename") string description, @part("file\"; filename=\"image.png\"") requestbody imgs); /** * 上传6张图片 * @param description * @param imgs1 * @param imgs2 * @param imgs3 * @param imgs4 * @param imgs5 * @param imgs6 * @return */ @multipart @post("/upload") call<string> uploadimage(@part("description") string description, @part("file\"; filename=\"image.png\"") requestbody imgs1, @part("file\"; filename=\"image.png\"") requestbody imgs2, @part("file\"; filename=\"image.png\"") requestbody imgs3, @part("file\"; filename=\"image.png\"") requestbody imgs4, @part("file\"; filename=\"image.png\"") requestbody imgs5, @part("file\"; filename=\"image.png\"") requestbody imgs6); } private static final retrofit sretrofit = new retrofit .builder() .baseurl(endpoint) .addconverterfactory(gsonconverterfactory.create()) // .addcalladapterfactory(rxjavacalladapterfactory.create()) // 使用rxjava作为回调适配器 .build(); private static final fileuploadservice apimanager = sretrofit.create(fileuploadservice.class); /** * 发说说 * @param paths * @param desp */ public static void upload(arraylist<string> paths,string desp){ requestbody[] requestbody= new requestbody[6]; if (paths.size()>0) { for (int i=0;i<paths.size();i++) { requestbody[i] = requestbody.create(mediatype.parse("multipart/form-data"), new file(paths.get(i))); } } call<string> call = apimanager.uploadimage( desp,requestbody[0],requestbody[1],requestbody[2],requestbody[3],requestbody[4],requestbody[5]); call.enqueue(new callback<string>() { @override public void onresponse(response<string> response, retrofit retrofit) { log.v("upload", response.message()); log.v("upload", "success"); } @override public void onfailure(throwable t) { log.e("upload", t.tostring()); } }); } }
效果:
关于android上传功能的更多内容请点击专题:android上传操作汇总进行学习。
本文已被整理到了《android微信开发教程汇总》,欢迎大家学习阅读。
以上就是本文的全部内容,希望对大家的学习有所帮助。