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

时间:2017-10-23 09:26:03

标签: c# android web-services wcf retrofit2

仍在寻找解决方案。任何人都知道如何做到这一点

我正在将图片从手机上传到服务。我成功上传了图片,但不知道如何保存

我正在使用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 webservice

[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下载wcf webservice的正确方法   改型

2 个答案:

答案 0 :(得分:0)

我在我的项目中使用了这个。它工作正常。

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);

答案 1 :(得分:-1)

// 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();
            }
        });
    }