1.NetApiService
//上传头像
@Multipart
@POST("file/upload")
Observable<BaseBean> uploadHeader(@Part("uid") RequestBody uid, @Part MultipartBody.Part file);
2.NetApi
//上传头像
public Observable<BaseBean> uploadHeader(RequestBody uid, MultipartBody.Part file) {
return netApiService.uploadHeader(uid, file);
}
3.presenter层
private NetApi netApi;
@Inject
public UploadHeaderPresenter(NetApi netApi) {
this.netApi = netApi;
}
@Override
public void uploadHeaders(String uid, String filePath) {
int i = filePath.lastIndexOf("/");
String fileName = filePath.substring(i + 1);
RequestBody file = RequestBody.create(MediaType.parse("application/octet-stream"), new File(filePath));
MediaType textType = MediaType.parse("text/plain");
RequestBody u = RequestBody.create(textType, uid);
MultipartBody.Part f = MultipartBody.Part.createFormData("file", fileName, file);
netApi.uploadHeader(u,f)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Observer<BaseBean>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(BaseBean baseBean) {
mView.uploadSuccess(baseBean);
}
@Override
public void onError(Throwable e) {
mView.uploadSuccess(null);
}
@Override
public void onComplete() {
}
});
}
4.MyFragment
package com.example.jingdong.ui.mine;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.jingdong.R;
import com.example.jingdong.activity.LoginActivity;
import com.example.jingdong.activity.UserInfoActivity;
import com.example.jingdong.util.SpUtil;
public class MyFragment extends Fragment{
private LinearLayout my_linear_login;
private RelativeLayout login_back_pic;
private ImageView my_user_icon;
private TextView my_user_name;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment_mine, null);
my_linear_login = view.findViewById(R.id.my_linear_login);
login_back_pic = view.findViewById(R.id.login_back_pic);
my_user_icon = view.findViewById(R.id.my_user_icon);
my_user_name = view.findViewById(R.id.my_user_name);
//设置监听
setListener();
return view;
}
private void setListener() {
my_linear_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String uid = SpUtil.getString(getActivity(), "uid", "");
if (TextUtils.isEmpty(uid)){
Intent intent = new Intent(getActivity(), LoginActivity.class);
startActivity(intent);
}else {
Intent intent = new Intent(getActivity(), UserInfoActivity.class);
startActivity(intent);
}
}
});
}
@Override
public void onResume() {
super.onResume();
String username = SpUtil.getString(getActivity(), "username", "");
String uid = SpUtil.getString(getActivity(), "uid", "");
String icon = SpUtil.getString(getActivity(), "icon", "");
if (TextUtils.isEmpty(uid)){
login_back_pic.setBackgroundResource(R.drawable.normal_regbg);
}else {
login_back_pic.setBackgroundResource(R.drawable.reg_bg);
}
if (!TextUtils.isEmpty(username)){
my_user_name.setText(username);
}else {
my_user_name.setText("登录/注册 >");
}
if (!TextUtils.isEmpty(icon)){
RequestOptions requestOptions = RequestOptions.circleCropTransform();
Glide.with(this).load(icon).apply(requestOptions).into(my_user_icon);
}else {
my_user_icon.setImageResource(R.drawable.user);
}
}
}
5.UserInfoActivity
package com.example.jingdong.activity;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.jingdong.R;
import com.example.jingdong.bean.BaseBean;
import com.example.jingdong.bean.UserInfoBean;
import com.example.jingdong.component.DaggerHttpComponent;
import com.example.jingdong.ui.base.BaseActivity;
import com.example.jingdong.ui.mine.contract.UploadHeaderContract;
import com.example.jingdong.ui.mine.presenter.UploadHeaderPresenter;
import com.example.jingdong.util.SpUtil;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class UserInfoActivity extends BaseActivity<UploadHeaderPresenter> implements UploadHeaderContract.View, View.OnClickListener {
private ImageView img_tou;
private TextView tv_name;
/**
* 退出登录
*/
private Button btn_tui;
private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
private static final int PHOTO_REQUEST_CUT = 3;// 结果
private String imgPath;
private File imgFile;
private PopupWindow popupWindow;
private LinearLayout ll2;
private Bitmap photo;
private String imgcropPath;
private File imgcropFile;
@Override
public int getContentLayout() {
return R.layout.activity_user_info;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
imgPath = getExternalCacheDir() + File.separator + "header.jpg";
imgcropPath = getExternalCacheDir() + File.separator + "header_crop.JPEG";
imgFile = new File(imgPath);
imgcropFile = new File(imgcropPath);
String username = SpUtil.getString(this, "username", "");
String icon = SpUtil.getString(this, "icon", "");
tv_name.setText(username);
RequestOptions requestOptions = RequestOptions.circleCropTransform();
Glide.with(this).load(icon).apply(requestOptions).into(img_tou);
}
private void initView() {
img_tou = (ImageView) findViewById(R.id.img_tou);
tv_name = (TextView) findViewById(R.id.tv_name);
btn_tui = (Button) findViewById(R.id.btn_tui);
ll2 = findViewById(R.id.ll2);
img_tou.setOnClickListener(this);
btn_tui.setOnClickListener(this);
}
@Override
public void inject() {
DaggerHttpComponent.builder()
.build()
.inject(this);
}
//上传成功的回调
@Override
public void uploadSuccess(BaseBean baseBean) {
if (baseBean==null){
Toast.makeText(this, "错误", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, baseBean.getMsg(), Toast.LENGTH_SHORT).show();
//上传头像后刷新用户信息
mPresenter.getUserInfo(SpUtil.getString(UserInfoActivity.this,"uid",""));
}
//获取用户信息成功的回调
@Override
public void getUserInfoSuccess(UserInfoBean userInfoBean) {
//先清空
SpUtil.clearSp(UserInfoActivity.this);
//登录成功后把主要信息存入sp
SpUtil.saveString(UserInfoActivity.this,"uid",userInfoBean.getData().getUid()+"");
SpUtil.saveString(UserInfoActivity.this,"username",userInfoBean.getData().getUsername());
SpUtil.saveString(UserInfoActivity.this,"token",userInfoBean.getData().getToken());
SpUtil.saveString(UserInfoActivity.this,"mobile",userInfoBean.getData().getMobile());
SpUtil.saveString(UserInfoActivity.this,"icon",userInfoBean.getData().getIcon()+"");
//重新设置本页的头像3
RequestOptions requestOptions = RequestOptions.circleCropTransform();
Glide.with(this).load(SpUtil.getString(UserInfoActivity.this,"icon","")).apply(requestOptions).into(img_tou);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_tui:
SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
sp.edit().clear().commit();
UserInfoActivity.this.finish();
break;
case R.id.img_tou:
//在底部弹出PopupWindow
initPopupWindow();
popupWindow.showAtLocation(ll2, Gravity.BOTTOM, 0, 0);
break;
case R.id.btn_cancel:
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
}
break;
case R.id.btn_take_photo:
//拍照拍照
takePhoto();
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
}
break;
case R.id.btn_pick_photo:
takePictureFromAlum();
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
}
break;
}
}
private void takePhoto() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgFile));
startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
}
//使用隐式意图打开系统相册
private void takePictureFromAlum() {
// Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// intent.setType("image/*");
// ComponentName componentName = intent.resolveActivity(getPackageManager());
// if (componentName != null) {
// startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
// }
Intent intent = new Intent();
//指定选择/获取的动作...PICK获取,拿
intent.setAction(Intent.ACTION_PICK);
//指定获取的数据的类型
intent.setType("image/*");
startActivityForResult(intent,PHOTO_REQUEST_GALLERY);
}
private void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra("return-data", true);
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PHOTO_REQUEST_TAKEPHOTO:
if (resultCode == Activity.RESULT_OK) {
//截取图片
startPhotoZoom(Uri.fromFile(imgFile));
}
break;
case PHOTO_REQUEST_GALLERY:
if (resultCode== Activity.RESULT_OK){
Uri data1 = data.getData();
startPhotoZoom(data1);
}
break;
case PHOTO_REQUEST_CUT:
//截图图片成功
Bundle bundle = data.getExtras();
if (bundle != null) {
photo = bundle.getParcelable("data");
//设置修改后的头像
// img_tou.setImageBitmap(photo);
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(imgcropFile));
photo.compress(Bitmap.CompressFormat.JPEG, 20, bos);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
//上传头像
mPresenter.uploadHeaders(SpUtil.getString(UserInfoActivity.this, "uid", ""), imgcropPath);
}
break;
}
}
private void initPopupWindow() {
View view = LayoutInflater.from(this).inflate(R.layout.pop_item, null);
popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout
.LayoutParams.WRAP_CONTENT);
//点击PopupWindow外部可以取消
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable());
// 设置背景颜色变暗
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0.7f;
getWindow().setAttributes(lp);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1f; //0.0-1.0
getWindow().setAttributes(lp);
}
});
Button btn_take_photo = view.findViewById(R.id.btn_take_photo);
Button btn_pick_photo = view.findViewById(R.id.btn_pick_photo);
Button btn_cancel = view.findViewById(R.id.btn_cancel);
btn_cancel.setOnClickListener(this);
btn_pick_photo.setOnClickListener(this);
btn_take_photo.setOnClickListener(this);
}
}
6.userinfolayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll2"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.UserInfoActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="头像"
android:layout_centerVertical="true"
android:layout_margin="5dp"
/>
<ImageView
android:id="@+id/img_tou"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_margin="3dp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="18sp"
android:layout_margin="5dp"
android:layout_centerVertical="true"
/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="123"
android:layout_margin="5dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<Button
android:id="@+id/btn_tui"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="退出登录"
android:layout_marginTop="200dp"
/>
</LinearLayout>