上传头像 + 日期选择器

布局文件




    

        

            

            
        

        

            

            
        

        

            

            
        

        

            

            
        

        

            

            
        

    

    

activity

public class MainActivity extends AppCompatActivity implements UserView {
    private static final int PHOTO_REQUEST_CAREMA = 1;// 拍照
    private static final int PHOTO_REQUEST_ALBUNM = 2;// 相册
    private static final int PHOTO_REQUEST_CUT = 3;// 结果
    @BindView(R.id.sim)
    SimpleDraweeView sim;
    private MultipartBody.Part part;
    @BindView(R.id.set_back)
    ImageView setBack;
    @BindView(R.id.avatar_layout)
    LinearLayout avatarLayout;
    @BindView(R.id.set_user_name)
    TextView setUserName;
    @BindView(R.id.text_name)
    TextView textName;
    @BindView(R.id.nick_name_layout)
    LinearLayout nickNameLayout;
    @BindView(R.id.set_time)
    TextView setTime;
    @BindView(R.id.set_sgin_out)
    Button setSginOut;
    private SharedPreferences sharedPreferences;
    private UserPresenter presenter;
    private String[] item = {"相机", "相册", "取消"};
    private String path = Environment.getExternalStorageDirectory() + "/JdPic.png";
    private String uid;
    private String token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        presenter = new UserPresenter(this);
        if (!TextUtils.isEmpty(uid)) {
            presenter.toUser(uid, token);
            initData();
        }
    }

    private void initData() {
        setTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 001 && resultCode == 2) {
            uid = data.getStringExtra("uid");
            Log.e("aaaaaaaaaaaa", uid + "");
            token = data.getStringExtra("token");
            presenter.toUser(uid, token);
        } else if (requestCode == PHOTO_REQUEST_CAREMA && resultCode == MainActivity.RESULT_OK) {
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(Uri.fromFile(new File(path)), "image/*");
            //设置是否支持裁剪
            intent.putExtra("crop", true);
            //设置框的宽高比
            intent.putExtra("aspactX", 1);
            intent.putExtra("aspacty", 1);
            //设置输出的图片
            intent.putExtra("outputX", 250);
            intent.putExtra("oupputY", 250);
            //将图片返回
            intent.putExtra("return-data", true);

            startActivityForResult(intent, PHOTO_REQUEST_CUT);
        } //设置裁剪
        else if (requestCode == PHOTO_REQUEST_ALBUNM && resultCode == MainActivity.RESULT_OK) {
            //得到图片路径
            Uri uri = data.getData();
            //调用系统裁剪功能
            Intent it = new Intent("com.android.camera.action.CROP");
            //得到照片进行裁剪
            it.setDataAndType(uri, "image/*");
            //设置是否支持裁剪
            it.putExtra("crop", true);
            //设置框的宽高比
            it.putExtra("aspactX", 1);
            it.putExtra("aspactY", 1);
            //设置输出图片大小
            it.putExtra("outputX", 250);
            it.putExtra("outputY", 250);
            //将图片返回
            it.putExtra("return-data", true);

            startActivityForResult(it, PHOTO_REQUEST_CUT);
        }
        //裁剪完后回到设置图片
        if (requestCode == PHOTO_REQUEST_CUT && resultCode == MainActivity.RESULT_OK) {

            Bitmap bitmap = data.getParcelableExtra("data");
            //获取文件路径
            File file = new File(this.getFilesDir().getAbsolutePath());
            if (!file.exists()) {
                //如果路径不存在就创建
                file.mkdirs();
            }
            //创建文件
            File file1 = new File(file, "photo.png");
            FileOutputStream fileOutputStream;
            try {
                //文件输出流
                fileOutputStream = new FileOutputStream(file1);
                //将bitmap写入文件流
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
                //刷新此输出流并强制将所有缓冲的输出字节被写出
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            //RequestBody封装了文件和文件的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file1);
            // MultipartBody.Part封装了接受的key和文件名字和RequestBody
            MultipartBody.Part part = MultipartBody.Part.createFormData("file", file1.getName(), requestBody);
            presenter.upLoad(uid, part);
        }
    }

    @Override
    public Context context() {
        return this;
    }


    /**
     * 用户信息
     *
     * @param userBean
     */
    @Override
    public void showUserBean(UserBean userBean) {
        textName.setText(userBean.getData().getNickname());
        Log.e("bbbbbbbbbb", userBean.getData().getIcon() + "");
        if (userBean.getData().getIcon() != null) {
            Uri parse = Uri.parse(userBean.getData().getIcon().replace("https", "http"));
            sim.setImageURI(parse);
        }
        sim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow();
            }
        });
    }

    //上传头像
    @Override
    public void showPart(UploadBean uploadBean) {
        Toast.makeText(this, uploadBean.getMsg() + "", Toast.LENGTH_LONG).show();
        presenter.toUser(uid, token);
    }

    //popupWindow
    private void showPopupWindow() {
        View view = LayoutInflater.from(this).inflate(R.layout.pop_avatar_layout, null);
        final PopupWindow popupWindow = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        popupWindow.setContentView(view);
        popupWindow.setFocusable(true);
        View rootview = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        popupWindow.showAtLocation(rootview, Gravity.CENTER, 0, 0);
        TextView openCarera = view.findViewById(R.id.open_camera);
        TextView openAlbum = view.findViewById(R.id.open_album);
        TextView openCancel = view.findViewById(R.id.open_cancel);
        openCarera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //相机
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                //将图片放到sd
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
                startActivityForResult(intent, PHOTO_REQUEST_CAREMA);
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
        openAlbum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //相册
                Intent intent1 = new Intent(Intent.ACTION_PICK);
                //设置图片的格式
                intent1.setType("image/*");
                startActivityForResult(intent1, PHOTO_REQUEST_ALBUNM);
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
        openCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
    }


    @OnClick({R.id.set_back, R.id.sim, R.id.text_name, R.id.set_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.set_back:
                break;
            case R.id.sim:
                if (TextUtils.isEmpty(uid)) {
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    startActivityForResult(intent, 001);
                }
                break;
            case R.id.text_name:
                break;
            case R.id.set_time:
                setTime.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                setTime.setText(String.format("%d:%d:%d",year,month,dayOfMonth));
                            }
                        },1997,8,19).show();
                    }
                });
                break;
        }
    }

api

public interface ApiService {
    //登录
    @FormUrlEncoded
    @POST(Constant.LOGIN_baseurl)
    Observable userLogin(@Field("mobile") String mobile, @Field("password") String password);
    //注册
    @FormUrlEncoded
    @POST(Constant.REGISTER_baseurl)
    Observable userRegist(@Field("mobile") String mobile, @Field("password") String password);

    @FormUrlEncoded
    @POST(Constant.USER_baseurl)
    Observable  toUser(@Field("uid") String uid,@Field("token") String token);

    //上传头像
    @Multipart
    @POST(Constant.UPLODE_baseurl)
    Observable upLoadPhoto(@Query("uid") String uid, @Part MultipartBody.Part part);
}

constant

public interface Constant {
    String baseurl="http://www.zhaoapi.cn/";
    String REGISTER_baseurl="user/reg";
    String LOGIN_baseurl="user/login";
    String USER_baseurl="user/getUserInfo";
    String UPLODE_baseurl = "file/upload";
}

 

你可能感兴趣的:(电商项目实战)