I got this endpoint but i don't know how to take photo from phone gallery and send this image via this endpoint?
Asked
Active
Viewed 35 times
-1
-
check this tutorial https://futurestud.io/tutorials/retrofit-2-how-to-upload-files-to-server, which helps you to send files to server using Retrofit – darwin Jul 12 '18 at 13:09
-
https://stackoverflow.com/questions/40008840/how-to-use-retrofit-2-to-send-file-and-other-params-together – Stefano Mtangoo Jul 12 '18 at 13:25
1 Answers
0
Try this code..
add below depedency into app level gradle file..
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
then define retrofit object..
public class ApiClient {
private final static String BASE_URL = "https://api.github.com";
public static ApiClient apiClient;
private Retrofit retrofit = null;
public static ApiClient getInstance() {
if (apiClient == null) {
apiClient = new ApiClient();
}
return apiClient;
}
//private static Retrofit storeRetrofit = null;
public Retrofit getClient() {
return getClient(null);
}
private Retrofit getClient(final Context context) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.readTimeout(60, TimeUnit.SECONDS);
client.writeTimeout(60, TimeUnit.SECONDS);
client.connectTimeout(60, TimeUnit.SECONDS);
client.addInterceptor(interceptor);
client.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
return chain.proceed(request);
}
});
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
}
make api interface..
public interface ApiInterface {
@Multipart
@POST("/signup/")
Call<UserResponseVo> registerUser(@PartMap Map<String, RequestBody> map);
}
and api calling into actiivty..
private void uploadImage() {
ApiInterface apiInterface= ApiClient.getInstance().getClient().create(ApiInterface.class);
file=new File(mAttachmentFilePath);
RequestBody mFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("profile_pic", file.getName(), mFile);
RequestBody userObjectId = RequestBody.create(MediaType.parse("text"), "HZucg1m6Gz");
Call<UserProfileResponse> userProfileResponseCall=apiInterface.uploadImage(fileToUpload,userObjectId);
showProgress();
userProfileResponseCall.enqueue(new Callback<UserProfileResponse>() {
@Override
public void onResponse(Call<UserProfileResponse> call, Response<UserProfileResponse> response) {
if (response!=null && response.body()!=null && response.isSuccessful()){
closeProgress();
Toast.makeText(getApplicationContext(),response.body().getMsg(),Toast.LENGTH_LONG).show();
Log.d("Response Message::",response.body().getMsg());
Glide.with(MainActivity.this).load(response.body().getAvatarUrl()).into(mIvServerImage);
}
}
@Override
public void onFailure(Call<UserProfileResponse> call, Throwable t) {
closeProgress();
Log.d("Error::",t.getMessage());
}
});
}
and i hope you know how to open gallery and get selected image path. and if you run app above marshallow device then add permission model.