最近做了选择多图并且上传服务器,在网上找了一些demo,适当的做了一下调整,用过了不能忘记,记下来以后还能多看看,本人觉得自己的博客有些渣渣,还希望大家不要介意啊,哪里有错误希望大家及时指正。
好了下面具体的分析一下:(想要做出功能,需求分析是必不可少的,需求、逻辑弄懂了再上手写代码,思路会很清晰的)
1.多图上传首先得选择图片(这里项目需求是既可以拍照上传也可以从相册中选择)
2.拍照上传很简单了网上也有很多例子,调用照相机,返回uri,获取图片
3.从相册中选择图片
3.1 获取手机中的所有图片
3.2 将图片存到自定义图片数组中显示
3.3 自定义ViewPager浏览图片
.
.
主要的逻辑大体是这样,下面具体看一下实现:
一、首先看一下界面:
<com.view.NoScrollGridView
android: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 {
@Id
public String imageId; //图片id
public 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 = thumbnailPat