繁体   English   中英

如何将图像从android改造上传到服务器

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM