[英]Upload Image to server using Retrofit2 POST request
I want to upload my gallery image to server using Retrofit2. 我想使用Retrofit2将图库图像上传到服务器。 API has 1 param which contains flie as form data & another param is header with key. API有1个参数,其中包含flie作为表单数据,另一个参数是带有密钥的标头。 When I am uploading image to server I get File not found message from server. 当我将图像上传到服务器时,我从服务器收到“找不到文件”消息。 I am sending proper file name but I don't know why it is giving so.Can anyone help me to solve.Below is my code. 我正在发送正确的文件名,但我不知道为什么会这样。任何人都可以帮助我解决。以下是我的代码。
final ProgressDialog progressDialog;
progressDialog = new ProgressDialog(UserProfileActivity.this);
progressDialog.setMessage("Updating Data");
progressDialog.show();
//Create Upload Server Client
ApiService service = RetroClient.getApiService();
//File creating from selected URL
File file = new File(imgDecodableString);
Log.d("TAG","file="+file.getName());
// create RequestBody instance from file
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
MultipartBody.Part.createFormData("image", file.getName(), requestFile);
Call<com.example.datamodel.Response> resultCall = service.uploadImage(loginHash,body);
resultCall.enqueue(new Callback<com.example.datamodel.Response>() {
@Override
public void onResponse(Call<com.example.datamodel.Response> call, retrofit2.Response<com.example.datamodel.Response> response) {
com.example.datamodel.Response mBean = response.body();
if(null != mBean)
{
Log.d("TAG","RS="+mBean.getMessage());
Log.d("TAG","RS="+mBean.getData().getvImage());
}
progressDialog.dismiss();
}
@Override
public void onFailure(Call<com.example.datamodel.Response> call, Throwable t) {
progressDialog.dismiss();
Log.d("TAG", "fail=" + t.getMessage());
}
});
RetroClient.java RetroClient.java
public class RetroClient {
public RetroClient() {
}
private static Retrofit getRetroClient() {
return new Retrofit.Builder()
.baseUrl(AppConstants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static ApiService getApiService() {
return getRetroClient().create(ApiService.class);
}}
ApiService.java ApiService.java
public interface ApiService {
@Multipart
@POST("api/User/uploadUserProfilePhoto")
Call<Response> uploadImage(@Header("vLoginHash") String vLoginHash,@Part MultipartBody.Part file);}
In logcat I am getting File not found message from server. 在logcat中,我从服务器收到“找不到文件”消息。 Anyone please help. 有人请帮忙。 Thanks in advance. 提前致谢。
Try without multipart 尝试不分手
@POST("/api/name.jpg")
Call<Object> upload(
@Header("header_name") String authorization,
@Body RequestBody body);
In OnActivityREsult 在OnActivityREsult中
if (resultCode == Activity.RESULT_OK ) {
Bitmap photo = null;
if(requestCode==REQUEST_GALLERY){
try {
photo = MediaStore.Images.Media.getBitmap(getActivity().getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
if(photo!=null){
Uri tempUri = getImageUri(getActivity(), photo);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
apimy.upload(header value,requestBody).enque...
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
assert cursor != null;
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
This code works fine for me. 这段代码对我来说很好用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.