I am working with Android Application. I am new to android development. In my application I like to upload the capture image to Server I am not having any idea in it. I have refer several link but I am not getting any idea. Please some body suggest me how to upload image using post method in Android SDK
Asked
Active
Viewed 392 times
3 Answers
1
You can use normal http technique to upload file to server in java using urlconnection class.
0
HttpPost httppost = new HttpPost(URL);
MultipartEntity entity = new MultipartEntity();
entity.addPart("title", new StringBody("yourfile", Charset.forName("UTF-8")));
File myFile = new File(Environment.getExternalStorageDirectory(), file);
FileBody fileBody = new FileBody(myFile);
entity.addPart("file", fileBody);
httppost.setEntity(entity);

user299654
- 36
- 3
0
Using retrofit 2 library to make the network call, you can send the image to the server using multipart First : create a method like the following in your interface
@Multipart
@POST("upload")
Call<ApiResponse> uploadImage(@Part MultipartBody.Part image);
Second : In your Activity where you get the picture use the following code before you call the uploadImage() method
File imageFile = new File(imagePath);
RequestBody requestBody =
RequestBody.create(MediaType.parse("multipart/form-data"), imageFile);
MultipartBody.Part formData =
MultipartBody.Part.createFormData("file", imageFile.getName(), requestBody);
Third : call the uploadImage() method and pass the formData as the parameter
See this tutorial as a reference : https://www.youtube.com/watch?v=0zpBhk3NG3Y&t=6s https://www.youtube.com/watch?v=RHvuSSwlTw4

Michael
- 411
- 2
- 6
- 15