Retrofit上传文件头像

//首先导入第三方的裁剪依赖库

compile 'com.soundcloud.android:android-crop:1.0.1@aar'

//清单文件配置

[java]  view plain  copy
  1. <activity android:name="com.soundcloud.android.crop.CropImageActivity" />  

//之后Retrofit的工具类 RetrofitUtils

 
[java]  view plain  copy
  1. public class RetrofitUtils {  
  2.     private static RetrofitUtils retrofitUtils;//工具类对象  
  3.     private static ApiFunction apiFunction;//请求网络接口  
  4.     public static OkHttpClient okHttpClient;  
  5.   
  6.     //静态快,获取OkHttpClient对象  
  7.     static {  
  8.         getOkHttpClient();  
  9.     }  
  10.   
  11.     //单例锁模式  
  12.     public static RetrofitUtils getInstence(){  
  13.         if(retrofitUtils==null){  
  14.             synchronized (RetrofitUtils.class){  
  15.                 if (retrofitUtils==null){  
  16.                     retrofitUtils=new RetrofitUtils();  
  17.                 }  
  18.             }  
  19.         }  
  20.         return retrofitUtils;  
  21.     }  
  22.     //单例模式获取okhttp  
  23.     public static OkHttpClient getOkHttpClient(){  
  24.         if(okHttpClient==null){  
  25.             synchronized (OkHttpClient.class){  
  26.                 if(okHttpClient==null){  
  27.                     File fileDir = new File(Environment.getExternalStorageDirectory(), "cache");  
  28.                     long fileSize = 10 * 1024 * 1024;  
  29.                     okHttpClient=new OkHttpClient.Builder()  
  30.                             .addInterceptor(new MyInter())  
  31.                             //打印拦截器日志  
  32.                             .addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))  
  33.                             .connectTimeout(15, TimeUnit.SECONDS)//设置连接超时时间  
  34.                             .readTimeout(15, TimeUnit.SECONDS)//设置读取超时时间  
  35.                             .writeTimeout(15, TimeUnit.SECONDS)//设置写入超时时间  
  36.                             //.cache(new Cache(fileDir,fileSize))//写入sd卡  
  37.                             .build();  
  38.                 }  
  39.             }  
  40.         }  
  41.         return okHttpClient;  
  42.     }  
  43.         //私有的无参构造  
  44.     private RetrofitUtils(){  
  45.         Retrofit retrofit=new Retrofit.Builder()  
  46.                 .baseUrl(Constants.Base_url)  
  47.                 .addConverterFactory(GsonConverterFactory.create())//添加gson转换器  
  48.                 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加rxjava转换器  
  49.                 .client(okHttpClient)//添加okhttp  
  50.                 .build();  
  51.         apiFunction=retrofit.create(ApiFunction.class);  
  52.     }  
  53.   
  54.     //获取  
  55.     public  ApiFunction API(){  
  56.         return apiFunction;  
  57.     }  
  58.   
  59.     //拦截器  
  60.     static class MyInter implements Interceptor {  
  61.   
  62.         private int versionCode;  
  63.         private Context context;  
  64.         private SharedPreferences jiLu;  
  65.         private String token;  
  66.   
  67.         @Override  
  68.         public Response intercept(Chain chain) throws IOException {  
  69.             Request request = chain.request();  
  70.             Request.Builder request_builder = request.newBuilder();  
  71.             context = MyApp.con;  
  72.             jiLu = context.getSharedPreferences("JiLu"0);  
  73.             if(jiLu!=null){  
  74.                 token = jiLu.getString("token"null);  
  75.             }  
  76.             try {  
  77.                 PackageManager pm = context.getPackageManager();  
  78.                 PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);  
  79.                 versionCode = pi.versionCode;  
  80.             } catch (PackageManager.NameNotFoundException e) {  
  81.                 e.printStackTrace();  
  82.             }  
  83.             if("GET".equals(request.method()))  
  84.             {  
  85.   
  86.                 HttpUrl.Builder builder = request.url().newBuilder();  
  87.                 HttpUrl build = builder.addQueryParameter("source", Constants.source)  
  88.                         .addQueryParameter("appVersion", versionCode + "")  
  89.                         .addQueryParameter("token", token + "")  
  90.                         .build();  
  91.                 request = request_builder.url(build).build();  
  92.   
  93.             }  
  94.   
  95.   
  96.             if ("POST".equals(request.method())) {  
  97.                 if (request.body() instanceof FormBody) {  
  98.                     System.out.println("FormBody开始添加公共参数");  
  99.                     FormBody.Builder builder = new FormBody.Builder();  
  100.                     FormBody body = (FormBody) request.body();  
  101.   
  102.                     for (int i = 0; i < body.size(); i++) {  
  103.                         builder.add(body.encodedName(i), body.encodedValue(i));  
  104.                     }  
  105.   
  106.                     body = builder.add("source", Constants.source)  
  107.                             .add("appVersion", String.valueOf(versionCode))  
  108.                             .add("token", token+"")  
  109.                             .build();  
  110.                     System.out.println("开始添加公共参数55555" );  
  111.                     request = request_builder.post(body).build();  
  112.   
  113.                 }  
  114.                 //因为传送文件要用到 @Multipart注解  
  115.                 else if(request.body() instanceof MultipartBody)  
  116.                 {  
  117.                     MultipartBody body = (MultipartBody) request.body();  
  118.                     MultipartBody.Builder builder=new MultipartBody.Builder().setType(MultipartBody.FORM);  
  119.                     builder.addFormDataPart("source","android")  
  120.                             .addFormDataPart("appVersion",versionCode+"")  
  121.                             .addFormDataPart("token",token+"");  
  122.                     List<MultipartBody.Part> parts = body.parts();  
  123.                     for (MultipartBody.Part part : parts) {  
  124.                         builder.addPart(part);  
  125.                     }  
  126.                     request=request_builder.post(builder.build()).build();  
  127.                 }  
  128.             }  
  129.   
  130.             return chain.proceed(request);  
  131.   
  132.         }  
  133.   
  134.         /** 
  135.          * 添加公共参数 
  136.          * 
  137.          * @param oldRequest 
  138.          * @return 
  139.          */  
  140.         private Request addParam(Request oldRequest) {  
  141.   
  142.             jiLu = context.getSharedPreferences("JiLu"0);  
  143.             if(jiLu!=null){  
  144.                 token = jiLu.getString("token"null);  
  145.             }  
  146.             PackageInfo packageArchiveInfo = MyApp.con.getPackageManager().getPackageArchiveInfo(MyApp.con.getPackageName(), 0);  
  147.             int versionCode = packageArchiveInfo.versionCode;  
  148.   
  149.             HttpUrl.Builder builder = oldRequest.url()  
  150.                     .newBuilder()  
  151.                     .setEncodedQueryParameter("source""android")  
  152.                     .setEncodedQueryParameter("token",token)  
  153.                     .setEncodedQueryParameter("appVersion",versionCode+"")  
  154.                     ;  
  155.   
  156.             Request newRequest = oldRequest.newBuilder()  
  157.                     .method(oldRequest.method(), oldRequest.body())  
  158.                     .url(builder.build())  
  159.                     .build();  
  160.   
  161.             return newRequest;  
  162.         }  
  163.     }  
  164. }  

//下面我们写 ApiFunction
[java]  view plain  copy
  1. public interface ApiFunction {  
  2.     //上传文件  
  3.     @Multipart  
  4.     @POST("file/upload")  
  5.     Observable<BaseUser> uploadFile(@PartMap Map<String, RequestBody> params);  
  6. }  

//接口工具类
[java]  view plain  copy
  1. public class Constants {  
  2.     public static String Base_url="https://www.zhaoapi.cn/";  
  3.     public static String UpLoad_url="file/upload";  
  4.     public static String source="android";  
  5. }  

//把文件转换RequestBody 添加到 Map<String, RequestBody> 中,用于请求数据
 
//工具类  
HttpParameterBuilder
[java]  view plain  copy
  1. public class HttpParameterBuilder {  
  2.   
  3.     private static HttpParameterBuilder mParameterBuilder;  
  4.     private static Map<String, RequestBody> params;  
  5.   
  6.     /** 
  7.      * 构建私有方法 
  8.      */  
  9.     private HttpParameterBuilder() {  
  10.   
  11.     }  
  12.   
  13.     /** 
  14.      * 初始化对象 
  15.      */  
  16.     public static HttpParameterBuilder newBuilder() {  
  17.         if (mParameterBuilder == null) {  
  18.             mParameterBuilder = new HttpParameterBuilder();  
  19.             if (params == null) {  
  20.                 params = new HashMap<>();  
  21.             }  
  22.         }  
  23.         return mParameterBuilder;  
  24.     }  
  25.   
  26.     /** 
  27.      * 添加参数 
  28.      * 根据传进来的Object对象来判断是String还是File类型的参数 
  29.      */  
  30.     public HttpParameterBuilder addParameter(String key, Object o) {  
  31.         if (o instanceof String) {  
  32.   
  33.             RequestBody body = RequestBody.create(MediaType.parse("text/plain"), (String) o);  
  34.             params.put(key, body);  
  35.         } else if (o instanceof File) {  
  36.             RequestBody body = RequestBody.create(MediaType.parse("image/*"), (File) o);  
  37.             params.put(key + "\"; filename=\"" + ((File) o).getName() + "", body);  
  38.         }  
  39.   
  40.         return this;  
  41.     }  
  42.   
  43.     /**  
  44.      * 初始化图片的Uri来构建参数  
  45.      * 一般不常用  
  46.      * 主要用在拍照和图库中获取图片路径的时候  
  47.      */  
  48.     public HttpParameterBuilder addFilesByUri(String key, List<Uri> uris) {  
  49.   
  50.         for (int i = 0; i < uris.size(); i++) {  
  51.             File file = new File(uris.get(i).getPath());  
  52.             RequestBody body = RequestBody.create(MediaType.parse("image/*"), file);  
  53.             params.put(key + i + "\"; filename=\"" + file.getName() + "", body);  
  54.         }  
  55.   
  56.         return this;  
  57.     }  
  58.   
  59.     /**  
  60.      * 构建RequestBody  
  61.      */  
  62.     public Map<String, RequestBody> bulider() {  
  63.   
  64.         return params;  
  65.     }  
  66. }  

//转换图片为uri 绝对路径的工具类 UtilsImageProcess
[java]  view plain  copy
  1. public class UtilsImageProcess {  
  2.     /** * 将得到的一个Bitmap保存到SD卡上,得到一个URI地址 */  
  3.     public static Uri saveBitmap(Bitmap bm) {  
  4.         //在SD卡上创建目录  
  5.         File tmpDir = new File(Environment.getExternalStorageDirectory() + "/org.chenlijian.test"); if (!tmpDir.exists()) {  
  6.             tmpDir.mkdir();  
  7.         }  
  8.         File img = new File(tmpDir.getAbsolutePath() + "test.png");  
  9.         try { FileOutputStream fos = new FileOutputStream(img);  
  10.             bm.compress(Bitmap.CompressFormat.PNG, 85, fos);  
  11.             fos.flush(); fos.close(); return Uri.fromFile(img);  
  12.         } catch (FileNotFoundException e) {  
  13.             e.printStackTrace(); return null;  
  14.         } catch (IOException e) {  
  15.             e.printStackTrace(); return null;  
  16.         }  
  17.     }  
  18.   
  19.     /** * 将得到的一个Bitmap保存到SD卡上,得到一个绝对路径 */  
  20.     public static String getPath(Bitmap bm) {  
  21.         //在SD卡上创建目录  
  22.         File tmpDir = new File(Environment.getExternalStorageDirectory() + "/org.chenlijian.test");  
  23.         if (!tmpDir.exists()) {  
  24.             tmpDir.mkdir();  
  25.         }  
  26.         File img = new File(tmpDir.getAbsolutePath() + "/test.png");  
  27.         try {  
  28.             FileOutputStream fos = new FileOutputStream(img);  
  29.             bm.compress(Bitmap.CompressFormat.PNG, 85, fos);  
  30.             fos.flush(); fos.close();  
  31.             return img.getCanonicalPath();  
  32.         } catch (FileNotFoundException e) {  
  33.             e.printStackTrace(); return null;  
  34.         } catch (IOException e) {  
  35.             e.printStackTrace(); return null;  
  36.         }  
  37.     }  
  38.   
  39.   
  40.     /** * 将图库中选取的图片的URI转化为URI */  
  41.     public static Uri convertUri(Uri uri, Context context) {  
  42.         InputStream is; try { is = context.getContentResolver().openInputStream(uri);  
  43.             Bitmap bitmap = BitmapFactory.decodeStream(is);  
  44.             is.close(); return saveBitmap(bitmap);  
  45.         } catch (FileNotFoundException e) {  
  46.             e.printStackTrace(); return null;  
  47.         } catch (IOException e) {  
  48.             e.printStackTrace(); return null;  
  49.         }  
  50.     }  
  51.   
  52.     /** * 在拍摄照片之前生成一个文件路径Uri,保证拍出来的照片没有被压缩太小,用日期作为文件名,确保唯一性 */  
  53.     public static String getSavePath(){  
  54.         String saveDir = Environment.getExternalStorageDirectory() + "/org.chenlijian.test";  
  55.         File dir = new File(saveDir);  
  56.         if (!dir.exists()) {  
  57.             dir.mkdir(); }  
  58.             Date date = new Date();  
  59.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");  
  60.         String fileName = saveDir + "/" + formatter.format(date) + ".png";  
  61.         return fileName;  
  62.     }  
  63.   
  64.     /** * 计算图片的缩放值 */  
  65.     public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {  
  66.         final int height = options.outHeight; final int width = options.outWidth;  
  67.         int inSampleSize = 1;  
  68.         if (height > reqHeight || width > reqWidth) {  
  69.             final int heightRatio = Math.round((float) height/ (float) reqHeight);  
  70.             final int widthRatio = Math.round((float) width / (float) reqWidth);  
  71.             inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;  
  72.         }  
  73.         return inSampleSize;  
  74.     }  
  75.   
  76.     /** * 根据路径获得图片并压缩,返回bitmap用于显示 */  
  77.     public static Bitmap getSmallBitmap(String filePath) {  
  78.         final BitmapFactory.Options options = new BitmapFactory.Options();  
  79.         options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options);  
  80.         // Calculate inSampleSize  
  81.         options.inSampleSize = calculateInSampleSize(options, 480800);  
  82.         // Decode bitmap with inSampleSize set  
  83.         options.inJustDecodeBounds = false;  
  84.         // 设置为true,画质更好一点,加载时间略长  
  85.         options.inPreferQualityOverSpeed = truereturn BitmapFactory.decodeFile(filePath, options);  
  86.     }  
  87. }  

//下面是我们的Model
[java]  view plain  copy
  1. public void UpLoad(@PartMap Map<String, RequestBody> params, final LoadListener loadListener) {  
  2.         RetrofitUtils.getInstence()  
  3.                 .API()  
  4.                 .uploadFile(params)  
  5.                 .subscribeOn(Schedulers.io())  
  6.                 .observeOn(AndroidSchedulers.mainThread())  
  7.                 .subscribe(new Consumer<BaseUser>() {  
  8.                     @Override  
  9.                     public void accept(BaseUser baseUser) throws Exception {  
  10.                              loadListener.Success(baseUser);  
  11.                     }  
  12.                 }, new Consumer<Throwable>() {  
  13.                     @Override  
  14.                     public void accept(Throwable throwable) throws Exception {  
  15.                              loadListener.Error(throwable.toString());  
  16.                     }  
  17.                 });  
  18.   
  19.    }  

//presenter
[java]  view plain  copy
  1. //修改头像  
  2.     @Override  
  3.     public void UpLoad(@PartMap Map<String, RequestBody> params) {  
  4.              iModel.UpLoad(params, new LoadListener() {  
  5.                  @Override  
  6.                  public void Success(Object o) {  
  7.                         showView.TouXiangSuccess(o);  
  8.                  }  
  9.   
  10.                  @Override  
  11.                  public void Error(String error) {  
  12.                         showView.Error(error);  
  13.                  }  
  14.              });  
  15.     }  


//之后是我们的view 

//头像的布局

[java]  view plain  copy
  1. <span style="font-size:18px;"> <com.facebook.drawee.view.SimpleDraweeView  
  2.                 app:roundAsCircle="true"  
  3.                 app:placeholderImage="@drawable/ic_yonghu"  
  4.                 android:layout_alignParentLeft="true"  
  5.                 android:layout_marginBottom="@dimen/y5"  
  6.                 android:id="@+id/touxiang"  
  7.                 android:layout_width="@dimen/x42"  
  8.                 android:layout_height="@dimen/y40"  
  9.                 /></span>  
//头像的点事件

[java]  view plain  copy
  1. <span style="font-size:18px;">case R.id.my_image_view:  
  2.                       //找到pop里面的选项  
  3.                       if(uid!=null){//判断是否登录  
  4.                           setPop();  
  5.                           menu.toggle();  
  6.                       }else{  
  7.                           Toast.makeText(this,"请登录账号",Toast.LENGTH_SHORT).show();  
  8.                       }  
  9.                       break;</span>  
//展示  popupWindow

[java]  view plain  copy
  1. <span style="font-size:18px;"//找到pop里面的选项  
  2.     private void setPop() {  
  3.         //弹出popupWindow  
  4.         v = LayoutInflater.from(this).inflate(R.layout.pp, null);  
  5.         //找到pop里面的选项  
  6.         Button pai = v.findViewById(R.id.btn_take_photo);  
  7.         Button xuan = v.findViewById(R.id.btn_pick_photo);  
  8.         Button qu =v.findViewById(R.id.btn_cancel);  
  9.         pai.setOnClickListener(this);  
  10.         xuan.setOnClickListener(this);  
  11.         qu.setOnClickListener(this);  
  12.         popupWindow = new PopupWindow(v, ViewGroup.LayoutParams.MATCH_PARENT,  
  13.                 ViewGroup.LayoutParams.WRAP_CONTENT);  
  14.         popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().  
  15.                 getColor(android.R.color.transparent)));  
  16.         popupWindow.setOutsideTouchable(true);  
  17.         //显示  
  18.         popupWindow.showAtLocation(ShowActivity.this.findViewById(R.id.main),  
  19.                 Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 00);  
  20.     }</span>  

//pp布局

[java]  view plain  copy
  1. <span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:orientation="vertical"  
  4.     android:layout_height="wrap_content">  
  5.   
  6.     <LinearLayout  
  7.         android:id="@+id/pop_layout"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentBottom="true"  
  11.         android:background="#9999"  
  12.         android:gravity="center_horizontal"  
  13.         android:orientation="vertical">  
  14.   
  15.   
  16.         <Button  
  17.             android:id="@+id/btn_take_photo"  
  18.             android:layout_width="fill_parent"  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_marginLeft="20dip"  
  21.             android:layout_marginRight="20dip"  
  22.             android:layout_marginTop="20dip"  
  23.             android:background="#8e8e8e"  
  24.             android:text="拍照"  
  25.             android:textStyle="bold" />  
  26.   
  27.         <Button  
  28.             android:id="@+id/btn_pick_photo"  
  29.             android:layout_width="fill_parent"  
  30.             android:layout_height="wrap_content"  
  31.             android:layout_marginLeft="20dip"  
  32.             android:layout_marginRight="20dip"  
  33.             android:layout_marginTop="5dip"  
  34.             android:background="#8e8e8e"  
  35.             android:text="从相册选择"  
  36.             />  
  37.   
  38.         <Button  
  39.             android:id="@+id/btn_cancel"  
  40.             android:layout_width="fill_parent"  
  41.             android:layout_height="wrap_content"  
  42.             android:layout_marginBottom="15dip"  
  43.             android:layout_marginLeft="20dip"  
  44.             android:layout_marginRight="20dip"  
  45.             android:layout_marginTop="15dip"  
  46.             android:background="#8e8e8e"  
  47.             android:text="取消"  
  48.             android:textColor="#ffffff"  
  49.   
  50.             />  
  51.     </LinearLayout>  
  52.   
  53. </LinearLayout>  
  54. </span>  
//pp的点击事件

[java]  view plain  copy
  1. <span style="font-size:18px;">@Override  
  2.     public void onClick(View v) {  
  3.         switch (v.getId()){  
  4.             //调用相机  
  5.             case R.id.btn_take_photo:  
  6.                 //首先创建一个路径  
  7.                 path= UtilsImageProcess.getSavePath();  
  8.                 //转换成uri路径  
  9.                 Uri uri1 = Uri.fromFile(new File(path));  
  10.                 //打开相机  
  11.                 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  12.                 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri1);  
  13.                 startActivityForResult(intent, 666);  
  14.                 popupWindow.dismiss();  
  15.                 break;  
  16.                 //调用相册  
  17.             case R.id.btn_pick_photo:  
  18.                 Intent in = new Intent(Intent.ACTION_GET_CONTENT);  
  19.                 //指定图片的类型  
  20.                 in.setType("image/*");  
  21.                 //打开相册  
  22.                 startActivityForResult(in, 999);  
  23.                 popupWindow.dismiss();  
  24.                 break;  
  25.             case R.id.btn_cancel:  
  26.                 Toast.makeText(this,"取消了",Toast.LENGTH_SHORT).show();  
  27.                 popupWindow.dismiss();  
  28.                 break;  
  29.         }  
  30.     }</span>  
//回调方法
[java]  view plain  copy
  1. <span style="font-size:18px;"//重写  
  2.     private static final String TAG = "MainActivity-----";  
  3.     @Override  
  4.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  5.         super.onActivityResult(requestCode, resultCode, data);  
  6.         //判断  
  7.         switch (requestCode) {  
  8.             case 666:  
  9.                 //拿到相片  
  10.                 Bitmap bitmap = UtilsImageProcess.getSmallBitmap(path);  
  11.                 //通过工具把相片转换成uri  
  12.                 Uri uri2 = UtilsImageProcess.saveBitmap(bitmap);  
  13.                 //调裁剪的第三方  
  14.                 beginCrop(uri2);  
  15.                 break;  
  16.             case 999:  
  17.                 //获取图片的地址,当更新成功之后,直接给控件赋值而不用再请求数据  
  18.                 Uri uri3 = UtilsImageProcess.convertUri(data.getData(),this);  
  19.                 //调裁剪的第三方  
  20.                 beginCrop(uri3);  
  21.                 break;  
  22.             case Crop.REQUEST_CROP:  
  23.                 //裁剪完成的方法  
  24.                 handleCrop(data);  
  25.                 break;  
  26.             default :  
  27.                 Toast.makeText(this,"取消了",Toast.LENGTH_SHORT).show();  
  28.                 break;  
  29.         }  
  30.     }</span>  

//调用裁剪的方法

[java]  view plain  copy
  1. //启动裁剪Activity  
  2.     private void beginCrop(Uri source) {  
  3.         Uri destination1 = Uri.fromFile(new File(UtilsImageProcess.getSavePath()));  
  4.         Crop.of(source, destination1).asSquare().start(this);  
  5.     }  
 
[java]  view plain  copy
  1. <span style="font-size:18px;">//处理裁剪得到的图片,上传到服务器  
  2.    private void handleCrop(Intent data) {  
  3.         Uri u = Crop.getOutput(data);  
  4.         try {  
  5.             //拿到剪裁之后的图片  
  6.             Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), u);  
  7.             //通过工具把bitmap转换为绝对路径,创建文件上传服务器  
  8.             String imagePath = UtilsImageProcess.getPath(bitmap);  
  9.             //通过工具类把bitmap转换为uri方便直接给控件赋值  
  10.             uri= UtilsImageProcess.saveBitmap(bitmap);  
  11.             Log.e("uri",uri+"");  
  12.             file = new File(imagePath);  
  13.             postFile(file);  
  14.         } catch (IOException e) {  
  15.             e.printStackTrace();  
  16.             Log.e("sadadas",e.toString());  
  17.         }  
  18.     }  
  19.   
  20.   
  21.     //上传头像的方法  
  22.     public void postFile(File file){  
  23.         //准备请求的参数,接口路径   HttpParameterBuilder工具类  
  24.         Map<String, RequestBody> params = HttpParameterBuilder.newBuilder()  
  25.                 .addParameter("uid",uid)  
  26.                 .addParameter("file",file)  
  27.                 .bulider();  
  28.         //调取工具类更新接口数据  
  29.         p.UpLoad(params);  
  30.     }</span>  

//更新成功之后记得刷新头像!!!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值