Android仿微信发表说说实现拍照、多图上传功能

Android仿微信发表说说实现拍照、多图上传功能,第1张

概述本文实例为大家分享了Android仿微信发表说说、心情功能,供大家参考,具体内容如下

本文实例为大家分享了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,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,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,VIEwGroup parent) {  VIEwHolder holder = null;  if (convertVIEw == null) {  holder = new VIEwHolder();    convertVIEw = inflater.inflate(R.layout.item_image,"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微信开发教程汇总》,欢迎大家学习阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助。

总结

以上是内存溢出为你收集整理的Android仿微信发表说说实现拍照、多图上传功能全部内容,希望文章能够帮你解决Android仿微信发表说说实现拍照、多图上传功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149622.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存