主页面的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="10dp"
android:textSize="20sp"
android:text="个人信息"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="@+id/wode_tv02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_marginLeft="25dp"
android:text="上传头像"/>
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/personal_img"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="40dp"
app:placeholderImage="@drawable/aaa"
android:layout_weight="7"
app:roundAsCircle="true" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#CCCCCC"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="@+id/wode_tv03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:text="昵称"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:layout_marginRight="10dp"
android:text="我是姐姐"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#CCCCCC"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="@+id/wode_tv04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:text="生日"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:layout_marginRight="10dp"
android:text="123456789"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="20dp"
android:background="#CCCCCC"/>
</LinearLayout>
</LinearLayout>
//弹出来的小框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="#cccccc"
android:orientation="vertical">
<Button android:id="@+id/mBtn2"
android:layout_width="370dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00dddddd"
android:text="相册"/>
<Button android:id="@+id/mBtn3"
android:layout_width="370dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00dddddd"
android:text="取消"/>
</LinearLayout>
</LinearLayout>
//接下来就要写接口了
//这是api的类
package bwie.com.lenovo.chenxiaojie201904299.api;
public class API {
public static final String BASE_URL = "http://172.17.8.100/small/";
public static final String USER_URL = "user/verify/v1/modifyHeadPic";
}
//这是一个实现类
public interface Apis {
@Multipart
@POST(API.USER_URL)
Observable<JsonBean> getimage(@QueryMap HashMap<String,String> prams, @Part MultipartBody.Part part);
}
//实现一个成功和失败的方法,方便后期调用
public interface IMainView<T> {
void onSuccess(T t);
void onFril(String msg);
}
//接下来要写网络请求框架了,retrofit框架
public class HttpUtils {
private static HttpUtils instance;
public final Apis apis;
private HttpUtils(){
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(8,TimeUnit.SECONDS)
.readTimeout(8,TimeUnit.SECONDS)
.writeTimeout(8,TimeUnit.SECONDS)
.addInterceptor(new HttpIntercepter())
.addNetworkInterceptor(httpLoggingInterceptor)
.build();
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.baseUrl(API.BASE_URL)
.build();
apis = retrofit.create(Apis.class);
}
public static HttpUtils getInstance(){
if (instance == null){
synchronized (HttpUtils.class){
if (instance == null){
instance = new HttpUtils();
}
}
}
return instance;
}
}
//网络封装框架还需要封装一个请求的类
public class HttpIntercepter implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request.Builder builder = request.newBuilder();
builder.addHeader("userId","11249");
builder.addHeader("sessionId","155056366467311249");
Request build = builder.build();
Response proceed = chain.proceed(build);
return proceed;
}
}
//接下来就要走MVP咯
//这是一个泛型类
public class BasePresenter<V> {
private V view;
public V getView() {
return view;
}
public void setView(V view) {
this.view = view;
}
public void detaCH(){
this.view = null;
}
}
//这是presenter的逻辑
public class ShangPresenter extends BasePresenter<DataView> {
public void doUplode(HashMap<String,String> prams, MultipartBody.Part part){
HttpUtils.getInstance().apis.getimage(prams,part)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<JsonBean>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(JsonBean jsonBean) {
DataView view = getView();
view.onSuccess(jsonBean);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}
//这是定义的一个view层的实现类
public interface DataView<T> {
void onSuccess(T t);
void onError(String msg);
}
//接下来要写主页里的代码
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.Toast;
import com.facebook.drawee.view.SimpleDraweeView;
import java.io.File;
import java.util.HashMap;
import bwie.com.lenovo.chenxiaojie201904299.mvp.presenter.ShangPresenter;
import bwie.com.lenovo.chenxiaojie201904299.mvp.view.DataView;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
public class MainActivity extends AppCompatActivity implements DataView {
private SimpleDraweeView personal_img;
private ShangPresenter shangPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
personal_img = findViewById(R.id.personal_img);
shangPresenter = new ShangPresenter();
shangPresenter.setView(this);
personal_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view = View.inflate(MainActivity.this, R.layout.pop_up, null);
final PopupWindow pop = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
pop.showAtLocation(view,Gravity.BOTTOM,0,0);
pop.showAsDropDown(v);
pop.setOutsideTouchable(true);
pop.setFocusable(true);
view.findViewById(R.id.mBtn2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent,222);
}
});
view.findViewById(R.id.mBtn3).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pop.dismiss();
}
});
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data!=null){
if (requestCode == 222){
Uri uri = data.getData();
personal_img.setImageURI(uri);
String[] arr = {MediaStore.Images.Media.DATA};
Cursor cursor = this.managedQuery(uri, arr, null, null, null);
int img_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String img_path = cursor.getString(img_index);
File file = new File(img_path);
if (file!=null&&file.exists()){
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file.getAbsoluteFile());
MultipartBody.Part part = MultipartBody.Part.createFormData("iamge", file.getName(), requestBody);
shangPresenter.doUplode(new HashMap<String, String>(),part);
}else {
Toast.makeText(MainActivity.this,"请选择文件",Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
public void onSuccess(Object object) {
}
@Override
public void onError(String msg) {
}
}
//权限有以下这些
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.CAMERA"/>
//不要忘了封装的bean类和Fresco的application类,application的类需要在清单文件注册一下