SoFunction
Updated on 2025-03-01

Android retrofit upload file instance (including avatar)

Uploading files mainly through interfaces

Add permissions

 <uses-permission android:name=""/>
 <uses-permission android:name=".READ_EXTERNAL_STORAGE"/>
 <uses-permission android:name=".WRITE_EXTERNAL_STORAGE"/>
 <uses-permission android:name=""/>
 <uses-permission android:name=".MOUNT_UNMOUNT_FILESYSTEMS"/>

Add dependencies

 //fresco
 compile ':fresco:+'
 //Support gif compile ':animated-gif:+'
 compile '.okhttp3:okhttp:3.9.1'
 compile ':gson:2.8.1'
 //retrofit
 compile '.retrofit2:retrofit:+'
 compile '.retrofit2:converter-gson:+'
 //Rxjava2
 compile '.rxjava2:rxjava:+'
 compile '.rxjava2:rxandroid:+'
 //Let retrofit support Rxjava2 compile '.retrofit2:adapter-rxjava2:+'
 compile '.okhttp3:logging-interceptor:3.9.1'

1. Network request

public class RetrofitUtils {
 //Custom path public static final String BASE_URL ="http://120.27.23.105/";
 private final Retrofit mRetrofit;
 public static class SINGLE_HOLDER{
 public static final RetiofitUtils INSTANCE = new RetiofitUtils(BASE_URL);
 }
 public static RetrofitUtils getInstance(){
 return SINGLE_HOLDER.INSTANCE;
 }
r
 private RetrofitUtils(String baseUrl){
 mRetrofit = buildRetrofit();
 }
 //
 private OkHttpClient buildOkHttpClient(){
 HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
 ();
 return new ()
  .connectTimeout(10000, )
  .addInterceptor(new Intercept())//Interceptor  .addInterceptor(logging)
  .build();
 }
 //Create retrofit private Retrofit buildRetrofit(){
 return new ()
  .client(buildOkHttpClient())
  .baseUrl(BASE_URL)
  .addConverterFactory(())
  .addCallAdapterFactory(())
  .build();
 }
 //Generics public &lt;T&gt; T create(Class&lt;T&gt; tClass){
 return (tClass);
 }
}

Interceptor:https:///article/

Path interface

public interface RetiofitVpi {
 //Find user information @GET("user/getUserInfo")
 Observable&lt;UserBean&gt; userBean(@Query("uid") String uid);
 //Upload file @Multipart
 @POST("file/upload")
 Observable&lt;FileBean&gt; uploadFile(@Query("uid") String uid,
     @Part("file\"; filename=\"") RequestBody file);   
}

Files under anim folder

&lt;!--android&lt;set&gt;Tags represent a series of frame animations,You can add animation effects to it --&gt;
&lt;set xmlns:andro &gt;
 &lt;translate
 android:duration="2000"
 android:fromYDelta="100%p"
 android:toYDelta="0" /&gt;
 &lt;alpha
 android:duration="2000"
 android:fromAlpha="0.0"
 android:toAlpha="1.0" /&gt;
&lt;/set&gt;
&lt;!--android&lt;set&gt;Tags represent a series of frame animations,You can add animation effects to it --&gt;
&lt;set xmlns:andro &gt;
 &lt;translate
 android:duration="2000"
 android:fromYDelta="100%p"
 android:toYDelta="0" /&gt;
 &lt;alpha
 android:duration="2000"
 android:fromAlpha="0.0"
 android:toAlpha="1.0" /&gt;
&lt;/set&gt;

layout

<
 android:layout_centerInParent="true"
 android:
 android:layout_width="300dp"
 android:layout_height="300dp"
 fresco:failureImage="@drawable/icon_failure"
 fresco:progressBarImage="@drawable/icon_placeholder"
 fresco:placeholderImage="@drawable/icon"
 fresco:progressBarAutoRotateInterval="1000"
 fresco:retryImageScaleType="centerInside"
 fresco:roundAsCircle="true"
 tools:layout_editor_absoluteY="41dp"
 tools:layout_editor_absoluteX="55dp"
 />

mainActiviy, requesting the user uid to replace the interface of the avatar

public class MainActivity extends AppCompatActivity {
 private SimpleDraweeView simple_drawee_view;
 private PopupWindow window;
 private String path;
 private Uri uri;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 //initialization (this);
 setContentView(.activity_main);
 //Get the layout of the avatar simple_drawee_view = (SimpleDraweeView) findViewById(.simple_drawee_view);
 path = () + "/";
 //Get uid Observable&lt;UserBean&gt; userInfo = userBean("3600");
 userInfo
  //Need to connect to the io child thread  .subscribeOn(())
  //Unable to update the UI in the main thread  .observeOn(())
  .subscribe(new Consumer&lt;UserBean&gt;() {
   @Override
   public void accept(UserBean nicknameBean) throws Exception {
    data = ();
   uri = (());
   simple_drawee_view.setImageURI(uri);
   }
  }, new Consumer&lt;Throwable&gt;() {
   @Override
   public void accept(Throwable throwable) throws Exception {
   ("MainActivity",());
   }
  });
 //Set click events for the controls that upload avatar simple_drawee_view.setOnClickListener(new () {
  @Override
  public void onClick(View view) {
  //Get popwindow to change the avatar  showPopwindow();
  }
 });
 }
 private Observable&lt;UserBean&gt; userBean(String uid) {
 RetiofitVpi iRetiofitVip = ().create();
 return (uid);
 }
 }

The above Android retrofit upload file example (including avatar) is all the content I share with you. I hope you can give you a reference and I hope you can support me more.