[英]How to upload Image to server from android retrofit
仍在尋找解決方案。 有人知道怎么做嗎
我正在將圖片從手機上傳到服務。 我成功上傳了圖片,但不知道如何保存
我正在使用compile 'com.squareup.retrofit2:retrofit:2.1.0'
改造方法
@Multipart
@POST("RestService/json/PostKYCDocImg/")
Call<UploadPictureResponse> getURLKYCDocImg(@Part MultipartBody.Part imageData);
在android上傳的文件中
File file = new File(new URI(de.getAttachFilePath()).getPath());
RequestBody mFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part image = MultipartBody.Part.createFormData("image", file.getName(), mFile);
Call<UploadPictureResponse> call = apiService.getURLKYCDocImg(image);
在Wcf Web服務中,我收到
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/json/PostKYCDocImg/", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
[FaultContract(typeof(ServiceData))]
string UploadPicture(Stream imageData);
public string UploadPicture(Stream imageData)
{
//saveImgInSpecPath(fileFullPath, GetBytesFromStream(imageData,
//System.Text.Encoding.UTF8), imageData);
saveImgInSpecPath2(fileFullPath, imageData);
}
private static Boolean saveImgInSpecPath2(string fileFullPath, Stream imageData)
{
try
{
//Save image here which is in imageData as stream and return saved status
//var fileStream = File.Create(fileFullPath);
//imageData.CopyTo(fileStream);
//StreamReader sr = new StreamReader(imageData.InputStream);
//var fileStream = File.Create(fileFullPath);
//imageData.InputStream.Seek(0, SeekOrigin.Begin);
//imageData.InputStream.CopyTo(fileStream);
//here i want to save image file which imageData in the form of stream
bool exists = File.Exists(fileFullPath);
return exists;
}
catch (Exception ex)
{
return false;
}
}
我在“ saveImgInSpecPath2”方法中嘗試了很多代碼,所有代碼均成功上傳到服務路徑,但保存錯誤
請建議正確的方法來從Android Retrofit保存在WCF Web服務中
我在項目中使用了這個。 而且它可以正常工作。
Stream str = File.OpenRead(@"C:\Users\a.asadi\Pictures\Capture.PNG");
FileStream fs = File.Create(@"C:\Users\a.asadi\Pictures\test.PNG", (int)str.Length);
byte[] bytes = new byte[str.Length];
str.Read(bytes, 0, bytes.Length);
fs.Write(bytes, 0, bytes.Length);
// retrofit method APIgetpost(interface java class)
@Multipart
@POST("RestService/json/PostKYCDocImg/")
Call<UploadPictureResponse> getURLKYCDocImg(@Part@Part MultipartBody.Part
file1);
// java類:
public void ProfileupdateExecute() {
ApiGetPost service = ApiConstant.getMainUrl().create(ApiGetPost.class);
JSONObject jTestObj = new JSONObject();
if (mediaPath != null) {
strImgaepath = mediaPath;
} else {
strImgaepath = "";
}
//File creating from selected URL
File file = new File(strImgaepath);
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
userimage =
MultipartBody.Part.createFormData("photo", file.getName(), requestFile);
try {
//photo field name
jTestObj.put("photo", strImgaepath);
} catch (JSONException e) {
e.printStackTrace();
}
updatecall = service.ProfileUpdate(userimage);
updatecall.enqueue(new Callback<UpdateTeacherProfileModel>() {
@Override
public void onResponse(Call<UpdateTeacherProfileModel> call, Response<UpdateTeacherProfileModel> response) {
viewDialog.hideDialog();
if (response.body()!= null) {
UpdateTeacherProfileModel userupdate = (response.body());
String Success = userupdate.getSuccess();
if (Success.equalsIgnoreCase("true")){
Toast.makeText(ProfileView_Activity.this, "Success", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ProfileView_Activity.this, "Check your credentials", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "Error..!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<UpdateTeacherProfileModel> call, Throwable t) {
viewDialog.hideDialog();
Toast.makeText(getApplicationContext(), "Please try again", Toast.LENGTH_SHORT).show();
call.cancel();
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.