有两种方式public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private String url = "http://172.16.52.17:8080/UploadDemo4/UploadFile";
private Button get_photo;
private GridView gv;
private List imageUrl;
private Bitmap bm;
private MyAdapter myAdapter;
private Button up_load;
private Intent intent;
private String imagePath;
private List list;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initAdapter();
}
private void initAdapter() {
myAdapter = new MyAdapter(imageUrl, bm, this);
gv.setAdapter(myAdapter);
}
private void initData() {
imageUrl = new ArrayList<>();
list = new ArrayList<>();
}
private void initView() {
get_photo = (Button) findViewById(R.id.get_photo);
get_photo.setOnClickListener(this);
gv = (GridView) findViewById(R.id.gv);
up_load = (Button) findViewById(R.id.up_load);
up_load.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.get_photo://打开相册intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);
break;
case R.id.up_load:
upLoad(list);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
Uri selectedImage = data.getData();
String[] filePathColumns = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePathColumns[0]);
imagePath = c.getString(columnIndex);
showImage(imagePath);
c.close();
break;
}
}
private void upLoad(List list) {
if (list != null) {
OkHttpClient okHttpClient = new OkHttpClient();
for (int i = 0; i < list.size(); i++) {
file = new File(list.get(i));
MultipartBody.Builder builder = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("img", file.getName(), RequestBody.create(MediaType.parse("image/jpeg"), file));
RequestBody requestBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("TAG", "onFailure: " + e);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.e("TAG", "成功" + response);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
}
private void showImage(String imagePath) {
list.add(imagePath);
bm = BitmapFactory.decodeFile(imagePath);
Log.e("TAG", "showImage:---- " + imagePath);
imageUrl.add(bm);
myAdapter.notifyDataSetChanged();
}
2.public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private String url = "http://172.16.52.17:8080/UploadDemo4/UploadFile";
private Button get_photo;
private Button up_load;
private GridView gv;
private List pathList;
private List bitmapList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initAdapter() {
MyAdapter myAdapter = new MyAdapter(pathList, this);
gv.setAdapter(myAdapter);
}
private void initView() {
get_photo = (Button) findViewById(R.id.get_photo);
up_load = (Button) findViewById(R.id.up_load);
gv = (GridView) findViewById(R.id.gv);
get_photo.setOnClickListener(this);
up_load.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.get_photo:
MultiImageSelector.create(this)
.showCamera(false) // 是否显示相机. 默认为显示
.count(9) // 最大选择图片数量, 默认为9. 只有在选择模式为多选时有效
//.single() // 单选模式
.multi() // 多选模式, 默认模式;
.start(this, 1);
break;
case R.id.up_load:
upload(pathList);
break;
}
}
private void upload(List list) {
if (list != null) {
OkHttpClient okHttpClient = new OkHttpClient();
for (int i = 0; i < list.size(); i++) {
File file = new File(list.get(i));
MultipartBody.Builder builder = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("img", file.getName(), RequestBody.create(MediaType.parse("image/jpeg"), file));
RequestBody requestBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("TAG", "onFailure: " + e);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.e("TAG", "成功" + response);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
// 获取返回的图片列表(存放的是图片路径)
pathList = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
// 处理你自己的逻辑 ....
initAdapter();
Log.d("tag", "" + pathList);
}
}