版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/lyhhj/article/details/47731439

最近做了选择多图并且上传服务器,在网上找了一些demo,适当的做了一下调整,用过了不能忘记,记下来以后还能多看看,本人觉得自己的博客有些渣渣,还希望大家不要介意啊,哪里有错误希望大家及时指正。

好了下面具体的分析一下:(想要做出功能,需求分析是必不可少的,需求、逻辑弄懂了再上手写代码,思路会很清晰的)

1.多图上传首先得选择图片(这里项目需求是既可以拍照上传也可以从相册中选择)

2.拍照上传很简单了网上也有很多例子,调用照相机,返回uri,获取图片

3.从相册中选择图片

3.1 获取手机中的所有图片

3.2 将图片存到自定义图片数组中显示

3.3 自定义ViewPager浏览图片

.

.

主要的逻辑大体是这样,下面具体看一下实现:

一、首先看一下界面:

<com.view.NoScrollGridViewandroid:id="@+id/noScrollgridview"android:layout_marginLeft="@dimen/smaller_space"android:layout_marginRight="@dimen/smaller_space"android:layout_width="match_parent"android:layout_height="wrap_content"android:horizontalSpacing="3dp"android:listSelector="@color/transparent"android:numColumns="3"android:scrollbars="none"android:layout_gravity="center"android:layout_marginTop="@dimen/smaller_space"android:verticalSpacing="5dp" />

是一个 NoScrollGridView,项目需要,所以用了不滚动的GridView,大家用GridView也是一样的。

noScrollgridview = (GridView) findViewById(R.id.noScrollgridview);noScrollgridview.setSelector(new ColorDrawable(Color.TRANSPARENT));/*新建传值给adapter*/if (file == null) {picAdapter = new PictureAdapter(this, 0, null);} else {//添加失败的图片到数组中for (int i=0;i<file.getMulFailFilePaths().length;i++){ImageItem imageItem = new ImageItem();imageItem.setImagePath(file.getMulFailFilePaths()[i].trim());Bimp.tempSelectBitmap.add(imageItem);}/*上传失败传值给adapter*/picAdapter = new PictureAdapter(this, 2, file);}

这个是初始化图片数组,适配器(新建、上传失败、上传成功的图片我用的都是一个adapter)

ImageItem是图片的模型,下面有它的属性

//从图库选择的图片model
public class ImageItem extends File implements Serializable {@Idpublic String imageId; //图片idpublic String thumbnailPath; public String imagePath; //图片路径private Bitmap bitmap; public boolean isSelected = false;public String getImageId() {return imageId;}public void setImageId(String imageId) {this.imageId = imageId;}public String getThumbnailPath() {return thumbnailPath;}public void setThumbnailPath(String thumbnailPath) {this.thumbnailPath = thumbnailPath;}public String getImagePath() {return imagePath;}public void setImagePath(String imagePath) {this.imagePath = imagePath;}public boolean isSelected() {return isSelected;}public void setSelected(boolean isSelected) {this.isSelected = isSelected;}
    /*根据图片路径获取图片的bitmap*/public Bitmap getBitmap() {            if(bitmap == null){try {bitmap = Bimp.revitionImageSize(imagePath);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return bitmap;}public void setBitmap(Bitmap bitmap) {this.bitmap = bitmap;}}

接下来是适配器:

由于涉及到添加图片,adapter中添加一个flag用来显示新建的图片,将选择的图片添加到公有的图片数组中,初始化的时候加载图片数组显示。(大家在看的时候可以忽略掉我的flag)

@SuppressLint("HandlerLeak")
public class PictureAdapter extends BaseAdapter {private LayoutInflater inflater;private int selectedPosition = -1;private boolean shape;private int flag = 0;//0 默认新建 1上传成功 2上传失败private AppItem_file file;public boolean isShape() {return shape;}private Activity context;public void setShape(boolean shape) {this.shape = shape;}public PictureAdapter(Activity context,int flag,AppItem_file file) {this.context = context;inflater = LayoutInflater.from(context);this.flag = flag;this.file = file;}//    public void update() {
//        loading();
//    }public int getCount() {if (flag==0){   //新建图片if (Bimp.tempSelectBitmap.size() == 6) {return 6;}return (Bimp.tempSelectBitmap.size() + 1);}else if (flag==1){  //上传成功return file.getFileList().size();}else {  //上传失败return file.getMulFailFilePaths().length;}}public Object getItem(int arg0) {if (flag==1){return file.getFileList().get(arg0);}else {return file.getMulFailFilePaths()[arg0];}}public long getItemId(int arg0) {return arg0;}public void setSelectedPosition(int position) {selectedPosition = position;}public int getSelectedPosition() {return selectedPosition;}public View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder = null;if (convertView == null) {//根据图片的数量加载不同布局if (getCount()==1&&flag!=0){convertView = inflater.inflate(R.layout.item_published_singal_item,parent, false);}else {convertView = inflater.inflate(R.layout.item_published_grida,parent, false);}holder = new ViewHolder();holder.image = (ImageView) convertView.findViewById(R.id.item_grida_image);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}/*** 根据初始化adapter传过来的flag值,去不同的地方找图片* flag=0,去Bimp的图片数组中找* flag=1,证明上传成功的,去下载好的getFileList中找* flag=2,为上传失败的,图片保存在FailFile中的List<ImageItem>中* by黄海杰 at:2015年7月16日 09:51:25* 优化图片显示* by黄海杰 at:2015年8月3日 17:09:01*/if (flag==0){   //新建图片if (position == Bimp.tempSelectBitmap.size()) {holder.image.setImageBitmap(BitmapFactory.decodeResource(convertView.getResources(), R.drawable.icon_add_pic_unfocused));if (position == 6) {if (flag==0){holder.image.setVisibility(View.GONE);}}} else {holder.image.setImageBitmap(Bimp.tempSelectBitmap.get(position).getBitmap());}}else if (flag==1){  //上传成功
//            List<Integer> ids = new ArrayList<Integer>();
//            for (int i=0;i<file.getFileList().size();i++){
//                ids.add(file.getFileList().get(i).getS_id());
//            }int id=file.getFileList().get(position).getS_id();try {
//                File file= NeedApplication.db.findById(File.class,id);String fileBigImgUri =NeedApplication.db.findById(File.class,id).getFileUriBig();if (fileBigImgUri!=null&&!"null".equals(fileBigImgUri))ImageLoader.getInstance().displayImage((fileBigImgUri).trim(),holder.image);} catch (DbException e) {e.printStackTrace();}}else {  //上传失败String url = "file://"+file.getMulFailFilePaths()[position].trim();ImageLoader.getInstance().displayImage(url, holder.image);}return convertView;}public class ViewHolder {public ImageView image;}}

下面是自定义相册界面,用来选择图片的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" android:background="#E1E0DE"><RelativeLayoutandroid:id="@+id/headview"android:layout_width="fill_parent"android:layout_height="45dp"android:background="@drawable/plugin_camera_title_bar"android:gravity="center_vertical"android:layout_marginBottom="3dp"><Buttonandroid:layout_width="60dp"android:layout_height="wrap_content"android:textSize="14sp"android:textColor="#fff"android:id="@+id/back"android:text="相册"android:background="@drawable/plugin_camera_title_btn_back"android:layout_marginTop="5dp"android:layout_marginBottom="5dp"android:layout_centerVertical="true"/><Buttonandroid:layout_width="67dp" android:layout_height="wrap_content"android:textSize="14sp"android:textColor="#fff"android:id="@+id/cancel"android:text="取消"android:background="@drawable/plugin_camera_title_btn_cancel"android:layout_centerVertical="true"android:layout_marginRight="5dp"android:layout_alignParentRight="true" /></RelativeLayout><RelativeLayoutandroid:id="@+id/bottom_layout"android:layout_width="match_parent"android:layout_height="50dp"android:layout_alignParentBottom="true"android:background="@drawable/plugin_camera_bottom_bar"android:layout_marginTop="3dp"><Buttonandroid:id="@+id/preview"android:layout_width="90dp"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:text="预览"android:background="@drawable/plugin_camera_title_btn_preview"/><Buttonandroid:id="@+id/ok_button"android:layout_width="90dp"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_marginRight="10dp"android:background="@drawable/plugin_camera_ok_btn_state"/></RelativeLayout><GridViewandroid:id="@+id/myGrid"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_above="@id/bottom_layout"android:layout_below="@id/headview"android:layout_centerInParent="true"android:layout_marginTop="2dp"android:horizontalSpacing="10dp"android:numColumns="4"android:verticalSpacing="10dp"android:background="#E1E0DE" /><TextViewandroid:id="@+id/myText"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="相册没有图片"android:textColor="@color/black"/></RelativeLayout>

同样也是用了GridView,获取手机中的图片,添加到数组中,显示

这里用到了一个AlbumHelper(相册帮助类),网上找的代码,感觉挺好的

public List<ImageBucket> getImagesBucketList(boolean refresh) {if (refresh || (!refresh && !hasBuildImagesBucketList)) {buildImagesBucketList();}List<ImageBucket> tmpList = new ArrayList<ImageBucket>();Iterator<Entry<String, ImageBucket>> itr = bucketList.entrySet().iterator();while (itr.hasNext()) {Entry<String, ImageBucket> entry = (Entry<String, ImageBucket>) itr.next();tmpList.add(entry.getValue());}return tmpList;}

获取到数据源之后,该显示了,也就是利用的获取图片帮助类里面的方法

// 初始化,给一些对象赋值private void init() {helper = AlbumHelper.getHelper();helper.init(getApplicationContext());contentList = helper.getImagesBucketList(false);dataList = new ArrayList<ImageItem>();for(int i = 0; i<contentList.size(); i++){dataList.addAll( contentList.get(i).imageList );}back = (Button) findViewById(R.id.back);cancel = (Button) findViewById(R.id.cancel);cancel.setOnClickListener(new CancelListener());back.setOnClickListener(new BackListener());preview = (Button) findViewById(R.id.preview);preview.setOnClickListener(new PreviewListener());intent = getIntent();Bundle bundle = intent.getExtras();gridView = (GridView) findViewById(R.id.myGrid);gridImageAdapter = new AlbumGridViewAdapter(mContext,dataList,Bimp.tempSelectBitmap);gridView.setAdapter(gridImageAdapter);tv = (TextView) findViewById(R.id.myText);gridView.setEmptyView(tv);okButton = (Button) findViewById(R.id.ok_button);okButton.setText("完成"+"(" + Bimp.tempSelectBitmap.size()+ "/"+ NeedApplication.picNums+")");}
gridImageAdapter.setOnItemClickListener(new AlbumGridViewAdapter.OnItemClickListener() {@Overridepublic void onItemClick(final ToggleButton toggleButton,int position, boolean isChecked,Button chooseBt) {if (Bimp.tempSelectBitmap.size() >= NeedApplication.picNums) {toggleButton.setChecked(false);chooseBt.setVisibility(View.GONE);if (!removeOneData(dataList.get(position))) {Toast.makeText(AlbumActivity.this, "超出可选图片张数", Toast.LENGTH_SHORT).show();}return;}if (isChecked) {chooseBt.setVisibility(View.VISIBLE);Bimp.tempSelectBitmap.add(dataList.get(position));okButton.setText("完成"+"(" + Bimp.tempSelectBitmap.size()+ "/"+ NeedApplication.picNums+")");} else {Bimp.tempSelectBitmap.remove(dataList.get(position));chooseBt.setVisibility(View.GONE);okButton.setText("完成"+"(" + Bimp.tempSelectBitmap.size() + "/"+ NeedApplication.picNums+")");}isShowOkBt();}});

点击图片选择加到公有图片数组中显示已选择

最后是预览图片,利用自定义viewpager,实现图片滑动.....代码就不占了,好多demo

下面贴两张截图吧:

【Android源码解析】选择多张图片上传多图预览相关推荐

  1. android h5 多图上传源码,JS移动端/H5同时选择多张图片上传并使用canvas压缩图片...

    最近在做一个H5的项目,里边涉及到拍照上传图片的功能以及识别图片的功能,这里对识别图片的功能不做赘述,不属本文范畴.我在做完并上线项目后,同事跟我提了一个要求是可不可以同时选择多张图片上传,我做的时候 ...

  2. Android源码解析--AlertDialog及AlertDialog.Builder

    昨天晚上弄到很晚,简单的看了下Dialog的源码,说要分析下建造者模式,在dialog里面的应用其实是在AlertDialog中. 按照惯例,先看类说明: [java] view plaincopy ...

  3. Android源码解析(一)动画篇-- Animator属性动画系统

    Android源码解析-动画篇 Android源码解析(一)动画篇-- Animator属性动画系统 Android源码解析(二)动画篇-- ObjectAnimator Android在3.0版本中 ...

  4. 一款动态跑路html源码,简单实用,上传解压就完事了

    介绍: 一款动态跑路源码,简单实用,上传解压就完事了 搭建教程: 1.领取主机 2.二级域名 3.上传源码到主机文件夹,解压 4.绑定域名,搭建完成 网盘下载地址: http://kekewangLu ...

  5. iOS学习:调用相机,选择图片上传,带预览功能

    iOS学习:调用相机,选择图片上传,带预览功能 发表于2年前(2013-05-30 21:38)   阅读( 18194) | 评论( 16) 27人收藏此文章,我要收藏 赞3 8月22日珠海 OSC ...

  6. html上传头像及预览,js实现头像上传并且可预览提交

    在用户注册账号或者修改资料的时候会需要用户在本地选择一张图片作为头像,并同时预览, 常见的思路有两种:一是将图片上传至服务器的临时文件夹中,并返回该图片的url,然后渲染在html页面:另一种思路是, ...

  7. struts上传word和excel文件到oracle,Struts文件上传,下载,重传,预览

    [Struts2]☆★之文件上传,下载,重传,预览 今日群里一兄弟问我能否给一份struts文件上传下载的例子,因为自己项目比较紧所以想在网上找 些源码给他,但是纵观全网,写的都不是太全,这让新手使用 ...

  8. js上传视频,预览视频

    js上传视频,预览视频 <videostyle="width:300px; height:auto;object-fit: fill;"playsinlinecontrols ...

  9. 微信小程序云开发实现上传文件和预览下载文件

    微信小程序云开发实现上传文件和预览下载文件 一.前言 目前微信提供了一个接口 wx.chooseMessageFile 它能让用户从聊天记录里面选择一个或者多个文件,然后返回它的一些信息,列入文件的p ...

最新文章

  1. 到底有哪些副业适合程序员或产品经理?
  2. 【arduino】亲测MAC上arduino安装ESP32 SPIFFS插件
  3. linux ntp 'ntp_request.c'远程拒绝服务漏洞,NTP 'ntp_request.c'远程拒绝服务漏洞
  4. mysql更改安装路径6_关于mysql安装后更改数据库路径方法-Centos6环境
  5. SQL语法中的JOIN类型
  6. 【翻译】CodeMix使用教程(七):扩展
  7. UTF-8 Unicode ANSI网页编码的区别
  8. psp3000 刷机
  9. win10激活时间(win10激活时间查询)
  10. 数据库的方向 - 行vs列
  11. caozuoqilai day4
  12. 双极性归零码 matlab,双极性不归零码的Matlab实现
  13. Hard link and soft link in Linux
  14. 如何关闭PPT、Word、Excel的预览图标
  15. C#基于 iTextSharp.text.pdf 实现PDF水印添加功能
  16. android 内功修炼参考
  17. 〖产品思维训练白宝书 - 产品思维认知篇⑤〗- 学习 [产品思维] 需要做哪些准备?
  18. Windows用户管理、文件权限、本地策略、组策略说明
  19. 60行代码爬取知乎神回复
  20. flash 与分解:分解百度MP3图片墙

热门文章

  1. 618快到了,给大家推荐一个购物比价脚本
  2. 爬取百度百科周星驰页面电影名称并生成词云图
  3. 如何将身份证扫描到A4纸上?这3个方法简单有效
  4. 信息安全服务资质:CCRC认证审核流程说明
  5. DNS污染检测方法 教你一招轻松验证DNS
  6. 使用 Rsync 实现远程同步,以及实时同步
  7. lsyncd和rsync实现文件实时同步
  8. ListView隔行变色
  9. 【报告分享】2021当代青年婚恋状态研究报告-极光大数据(附下载)
  10. Sigmoid、Tanh、ReLU、Leaky_ReLU、SiLU、Mish函数python实现