效果
![](https://img-my.csdn.net/uploads/201710/13/1507886609_1302.gif)
代码
public class MainActivity extends AppCompatActivity {
ListView listview;
AlbumList albumList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
}
private void initData() {
List<AlbumList.ImgListBean> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
List<String> pictures = ImgHandleUtils.getPictures(Environment.getExternalStorageDirectory() + "/新文件夹");
AlbumList.ImgListBean imgListBean = new AlbumList.ImgListBean();
imgListBean.setImgUrlList(pictures);
imgListBean.setDirName("金地小城");
list.add(imgListBean);
}
albumList = new AlbumList();
albumList.setImgList(list);
}
private void initView() {
listview = (ListView) findViewById(R.id.listview);
ListViewAdapter adapter = new ListViewAdapter(this, albumList.getImgList());
listview.setAdapter(adapter);
}
}
public class ListViewAdapter extends BaseAdapter {
private Context mContext;
private List<AlbumList.ImgListBean> imgList;
//指定upToken, 强烈建议从服务端提供get请求获取, 这里为了演示直接指定key
private static String uptoken = "um6IEH7mtwnwkGpjImD08JdxlvViuELhI4mFfoeL:NCnWTr1D3jR_9_dxj6geIgPBQq4=:eyJzY29wZSI6ImFuZHJvaWRkZW1vIiwiZGVhZGxpbmUiOjIzMDk5MjE5MTIsInJldHVybkJvZHkiOiJ7IFwiaGFzaFwiOiQoZXRhZyksXCJrZXlcIjokKGtleSksXCJtaW1lVHlwZVwiOiQobWltZVR5cGUpLCBcImZzaXplXCI6JChmc2l6ZSl9In0=";
private UploadManager uploadManager; //七牛SDK的上传管理者
private UploadOptions uploadOptions; //七牛SDK的上传选项
private UpProgressHandler upProgressHandler; //七牛SDK的上传进度监听
private UpCancellationSignal upCancellationSignal; //七牛SDK的上传过程取消监听
private boolean isProgressCancel = false; //网络请求过程中是否取消上传或下载
public ListViewAdapter(Context context, List<AlbumList.ImgListBean> imgList) {
this.mContext = context;
this.imgList = imgList;
}
@Override
public int getCount() {
return imgList.size();
}
@Override
public Object getItem(int i) {
return imgList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
final ViewHolder holder;
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.listview_item, viewGroup, false);
holder = new ViewHolder();
holder.item_img = view.findViewById(R.id.item_img);
holder.item_tv_name = view.findViewById(R.id.item_tv_name);
holder.item_tv_count = view.findViewById(R.id.item_tv_count);
holder.probar_upload = view.findViewById(R.id.probar_upload);
holder.tv_progress = view.findViewById(R.id.tv_progress);
holder.tv_upload = view.findViewById(R.id.tv_upload);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
final AlbumList.ImgListBean imgListBean = imgList.get(i);
Glide.with(mContext)
.load(new File(imgListBean.getImgUrlList().get(0)))
.placeholder(R.mipmap.ic_launcher)
.into(holder.item_img);
holder.item_tv_name.setText(imgListBean.getDirName());
holder.item_tv_count.setText(imgListBean.getImgUrlList().size() + "");
holder.tv_upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (holder.tv_upload.getText().equals("上传")) {
holder.tv_upload.setText("暂停");
//开始上传
holder.probar_upload.setMax(imgListBean.getImgUrlList().size());
if (!isInitUpload) {
initUpload();
}
upload(imgListBean.getImgUrlList(), holder.probar_upload, holder.tv_progress, holder.tv_upload);
} else if (holder.tv_upload.getText().equals("暂停")) {
holder.tv_upload.setText("继续");
//暂停上传
isProgressCancel = true;
Toast.makeText(mContext, (currentIndex + ""), Toast.LENGTH_SHORT).show();
} else if (holder.tv_upload.getText().equals("继续")) {
holder.tv_upload.setText("暂停");
//继续上传
Toast.makeText(mContext, (currentIndex + ""), Toast.LENGTH_SHORT).show();
isProgressCancel = false;
upload(imgListBean.getImgUrlList(), holder.probar_upload, holder.tv_progress, holder.tv_upload);
}
}
});
return view;
}
private int currentIndex = 0;//当前上传到第几张照片,默认从0开始
private boolean isInitUpload = false;
/**
* 上传方法
*
* @param imgUrlList 单个相册的图片路径集合
* @param probar_upload
* @param tv_progress
*/
private void upload(final List<String> imgUrlList, final ProgressBar probar_upload, final TextView tv_progress, final TextView tv_upload) {
uploadManager.put(new File(imgUrlList.get(currentIndex)), null, uptoken, new UpCompletionHandler() {
/**
* 主线程执行
*
* @param key UploadManager.put()方法中指定的key,也就是上传到七牛服务器上的文件名;
* @param info Json串表示的上传信息,包括使用版本,请求状态,请求Id等信息;
* @param response Json串表示的文件信息,包括文件Hash码,保存在七牛服务的key,文件Mime类型,文件大小等信息,具体字段取决上传策略的设置;
*/
@Override
public void complete(String key, ResponseInfo info, JSONObject response) {
if (info.isOK()) {
Log.e("QINIULOG:", key + "!\n" + info + "!\n" + response + "!");
currentIndex++;
probar_upload.setProgress(currentIndex);
tv_progress.setText(currentIndex + "/" + imgUrlList.size());
if (currentIndex < imgUrlList.size()) {
upload(imgUrlList, probar_upload, tv_progress, tv_upload);//递归上传
} else {
currentIndex = 0;//重置
tv_upload.setText("上传完成");
}
} else {
Log.e("QINIULOG:", "上传失败:" + info.statusCode);
}
}
}, uploadOptions);
}
private static class ViewHolder {
private ImageView item_img;
private TextView item_tv_name;
private TextView item_tv_count;
private ProgressBar probar_upload;
private TextView tv_progress;
private TextView tv_upload;
}
/**
* 初始化上传配置,只需初始化一次就可以
*/
private void initUpload() {
isInitUpload = true;
uploadManager = new UploadManager();
upProgressHandler = new UpProgressHandler() {
/**
* 主线程执行
* @param key 上传时的upKey;
* @param percent 上传进度;
*/
@Override
public void progress(String key, double percent) {
//上传进度相关
}
};
upCancellationSignal = new UpCancellationSignal() {
@Override
public boolean isCancelled() {
//控制取消上传,取消之后就不能再上传了
return isProgressCancel;
}
};
//定义数据或文件上传时的可选项
uploadOptions = new UploadOptions(
null, //扩展参数,以x:开头的用户自定义参数
"mime_type", //指定上传文件的MimeType
true, //是否启用上传内容crc32校验
upProgressHandler, //上传内容进度处理
upCancellationSignal //取消上传信号
);
}
}
上传成功后七牛返回的信息
ResponseInfo info 信息
{ver:7.3.10,
ResponseInfo:1507880162533195,
status:200,
reqId:a30AAJ89bN4BEe0U,
xlog:body:1;s.ph;s.put.tw;s.put.tr:2;s.put.tw;s.put.tr:5;s.ph;PFDS:5;PFDS:7;rs36_16.sel:4/not found;rdb.g:1/no such key;DBD:1/404;v4.get:1/Document not found;rs36_16.ins:9;rwro.ins:15;RS:16;rs.put:16;rs-upload.putFile:24;UP:28,
xvia:bc488,
vdn-jssz-mob-1-6,
host:upload.qiniup.com,
path:/,
ip:xxxxxxx,
port:80,
duration:214 s,
time:1507880164,
sent:233531,
error:null}
JSONObject response 信息
{"hash":"Fsu2LPwvk331ltLR50VLT2FHpFxB",
"key":"Fsu2LPwvk331ltLR50VLT2FHpFxB",
"mimeType":"image\/jpeg",
"fsize":232869}
注意事项:
七牛对上传的图片做了限制,当图片大于4M的时候,会自动使用断点上传,上传过程会比较慢,如果小于4M,会使用Form上传,速度比较快。所以如果允许的话尽量将图片大小控制在4M以内,方便上传。