博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图片上传
阅读量:4693 次
发布时间:2019-06-09

本文共 4095 字,大约阅读时间需要 13 分钟。

public class UpLoadActivity extends TakePhotoActivity implements ContractClass.ReleaseFinish {
@BindView(R.id.titleview) TitleView titleview; @BindView(R.id.edittext) EditText edittext; @BindView(R.id.recyclerview) RecyclerView recyclerview; @BindView(R.id.imageview) ImageView imageview; @BindView(R.id.intentttt) Button intentttt; private List
parts = new ArrayList<>(); private List
pics = new ArrayList<>(); private MyAdapter myAdapter; private UpLoadPresenter upLoadPresenter; private int userId; private String sessionId; @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_upload); ButterKnife.bind(this); Intent intent = getIntent(); userId = intent.getIntExtra("userId", 0); sessionId = intent.getStringExtra("sessionId"); myAdapter = new MyAdapter(pics); recyclerview.setLayoutManager(new GridLayoutManager(this, 3)); recyclerview.setAdapter(myAdapter); upLoadPresenter = new UpLoadPresenter(); upLoadPresenter.onAttch(this); Map
maphead = new HashMap<>(); maphead.put("userId", userId); maphead.put("sessionId", sessionId); Map
mapbody = new HashMap<>(); mapbody.put("commodityId", 1); mapbody.put("content", "推送图片"); titleview.setSetOnClick(new TitleView.SetOnClick() {
@Override public void clickimage(View view) {
finish(); } @Override public void clickrightimage(View view) {
if (pics.size() == 0) {
Toast.makeText(UpLoadActivity.this, "还没有选择照片哦", Toast.LENGTH_SHORT).show(); } else {
upLoadPresenter.beginLogin(maphead, mapbody, parts); } } }); imageview.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
//获取TakePhoto对象 TakePhoto takePhoto = getTakePhoto(); //从图片库选择图片 takePhoto.onPickFromGallery(); } }); } /** * 接受图片结果 *

* 绝对路径 -》 文件 -》 RequestyBody -》 Part -》 添加到List

->调用接口 * * @param result */ @Override public void takeSuccess(TResult result) { super.takeSuccess(result); if (parts.size() < 4) { TImage image = result.getImage(); //拿到单个图片的绝对路径 String originalPath = image.getOriginalPath(); //把路径转换成文件 File file = new File(originalPath); pics.add(file); myAdapter.notifyDataSetChanged(); //图片类型 通过parse方法 MediaType parse = MediaType.parse("image/*"); Log.i("TAG", "takeSuccess: " + parse); //根据类型和文件构造一个请求体 通过create方法 RequestBody requestBody = RequestBody.create(parse, file); //根据key、文件名字、请求体构造一个part对象 通过createFormData MultipartBody.Part part = MultipartBody.Part.createFormData("image", file.getName(), requestBody); parts.add(part); } else { Toast.makeText(this, "不能再多了", Toast.LENGTH_SHORT).show(); } } @Override public void success(Object o) { UpLoadBean upLoadBean = (UpLoadBean) o; Toast.makeText(this, "" + upLoadBean.getMessage(), Toast.LENGTH_SHORT).show(); Intent intent = new Intent(UpLoadActivity.this, ShowActivity.class); intent.putExtra("userId", userId); intent.putExtra("sessionId", sessionId); startActivity(intent); finish(); } @OnClick(R.id.intentttt) public void onViewClicked() { Intent intent = new Intent(UpLoadActivity.this, ShowActivity.class); intent.putExtra("userId", userId); intent.putExtra("sessionId", sessionId); startActivity(intent); finish(); } }

//图片上传需要的注解和正常的有所差别 @Multipart @POST("/small/circle/verify/v1/releaseCircle") Observable
Release(@HeaderMap Map
maphead, @QueryMap Map
mapbody, @Part List
parts);

转载于:https://www.cnblogs.com/fybb/p/11576963.html

你可能感兴趣的文章