废话不多说直接上图
好,这就是以上的效果图。在这里要吐槽一下网上的有些博主,既然准备写博客,那就拿点干货出来,别老是藏着掖着,要是不想分享就别写博客,写一点藏一点浪费自己也浪费别人的时间啊。
我这个demo是我根据选取的图片然后获取路径最后上传到七牛的服务器上的功能。
好了,贴上关键代码,注释什么的都写的很详细,大家应该可以看懂;
package com.example.leeandroid.photopickerlee;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.bumptech.glide.Glide;
import com.foamtrace.photopicker.ImageCaptureManager;
import com.foamtrace.photopicker.PhotoPickerActivity;
import com.foamtrace.photopicker.PhotoPreviewActivity;
import com.foamtrace.photopicker.SelectModel;
import com.foamtrace.photopicker.intent.PhotoPickerIntent;
import com.foamtrace.photopicker.intent.PhotoPreviewIntent;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private Button btnMuilt; // 多选
private Button up; // 上传
private static final int REQUEST_CAMERA_CODE = 11;
private static final int REQUEST_PREVIEW_CODE = 22;
private ArrayList imagePaths = null;
private ArrayList totalList=new ArrayList<>();
private ImageCaptureManager captureManager; // 相机拍照处理类
private GridView gridView;
private int columnWidth;
private GridAdapter gridAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnMuilt= (Button) findViewById(R.id.btnMuilt);
up= (Button) findViewById(R.id.btnSingle);
gridView= (GridView) findViewById(R.id.gridView);
int cols = getResources().getDisplayMetrics().widthPixels / getResources().getDisplayMetrics().densityDpi;
cols = cols < 3 ? 3 : cols;
gridView.setNumColumns(cols);
// Item Width
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int columnSpace = getResources().getDimensionPixelOffset(R.dimen.space_size);
columnWidth = (screenWidth - columnSpace * (cols-1)) / cols;
// preview
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
PhotoPreviewIntent intent = new PhotoPreviewIntent(MainActivity.this);
intent.setCurrentItem(position);
intent.setPhotoPaths(imagePaths);
startActivityForResult(intent, REQUEST_PREVIEW_CODE);
}
});
btnMuilt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PhotoPickerIntent intent = new PhotoPickerIntent(MainActivity.this);
intent.setSelectModel(SelectModel.MULTI);
intent.setShowCarema(true); // 是否显示拍照
intent.setMaxTotal(9); // 最多选择照片数量,默认为9
intent.setSelectedPaths(imagePaths); // 已选中的照片地址, 用于回显选中状态
startActivityForResult(intent, REQUEST_CAMERA_CODE);
}
});
up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i=0;i paths = new ArrayList<>();
paths.add(captureManager.getCurrentPhotoPath());
loadAdpater(paths);
}
break;
}
}
}
private void loadAdpater(ArrayList paths){
if(imagePaths == null){
imagePaths = new ArrayList<>();
}
imagePaths.clear();
imagePaths.addAll(paths);
try{
JSONArray obj = new JSONArray(imagePaths);
Log.e("--", obj.toString());
if (obj!=null){
for (int i = 0; i < obj.length(); i++) {
totalList.add(obj.get(i).toString());
}
}
}catch (Exception e){
e.printStackTrace();
}
if(gridAdapter == null){
gridAdapter = new GridAdapter(imagePaths);
gridView.setAdapter(gridAdapter);
}else {
gridAdapter.notifyDataSetChanged();
}
}
//上传七牛
private void uploadImageToQiniu(String filePath, String token) {
UploadManager uploadManager = new UploadManager();
// 设置图片名字
// SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
// String key = "icon_" + sdf.format(new Date());
uploadManager.put(filePath, null, token, new UpCompletionHandler() {
@Override
public void complete(String key, ResponseInfo info, JSONObject res) {
// info.error中包含了错误信息,可打印调试
if (info.statusCode==200){
Log.i("--completed","completed");
Log.i("--res",res+"");
}else {
Log.i("--uncomplete","uncomplete");
}
}
}, null);
}
private class GridAdapter extends BaseAdapter {
private ArrayList listUrls;
public GridAdapter(ArrayList listUrls) {
this.listUrls = listUrls;
}
@Override
public int getCount() {
return listUrls.size();
}
@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) {
ImageView imageView;
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.item_image, null);
imageView = (ImageView) convertView.findViewById(R.id.imageView);
convertView.setTag(imageView);
// 重置ImageView宽高
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(columnWidth, columnWidth);
imageView.setLayoutParams(params);
}else {
imageView = (ImageView) convertView.getTag();
}
Glide.with(MainActivity.this)
.load(new File(getItem(position)))
.placeholder(R.mipmap.default_error)
.error(R.mipmap.default_error)
.centerCrop()
.crossFade()
.into(imageView);
return convertView;
}
}
}
//*********注意********//
这里面我使用了photopicker这个三方控件,还有就是七牛的使用要配置好环境,注意这两点,相信你下次也可以轻松的解决这个类型的需求了。
下面附上源码https://github.com/leepears/UpPhotoToQN