图片上传

梦想的初衷 提交于 2019-11-30 09:40:23
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<MultipartBody.Part> parts = new ArrayList<>();    private List<File> 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<String, Object> maphead = new HashMap<>();        maphead.put("userId", userId);        maphead.put("sessionId", sessionId);        Map<String, Object> 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();            }        });    }    /**     * 接受图片结果     * <p>     * 绝对路径 -》 文件 -》 RequestyBody -》 Part  -》 添加到List<Part> ->调用接口     *     * @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<UpLoadBean> Release(@HeaderMap Map<String,Object> maphead, @QueryMap Map<String,Object> mapbody, @Part List<MultipartBody.Part> parts);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!