Uploading image from android to server using c# .net

2k views Asked by At

I'm having problem to upload photo from an Android app to server using c#.net here is my code so far... : CLIENT SIDE (ANDROID)

EDIT

class ImageUploadTask extends AsyncTask<Void, Void, String> {

    private String webAddressToPost = "http://myIP/Glasses/api/Photo/";

    @Override
    protected String doInBackground(Void... params) {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(webAddressToPost);

            MultipartEntity entity = new MultipartEntity();

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, 100, bos);
            byte[] data = bos.toByteArray();
            byte[] file = Base64.encode(data, Base64.DEFAULT);
            string filecode = new String(file);

            entity.addPart("uploaded", new StringBody(filecode));


            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost, localContext);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            response.getEntity().getContent(), "UTF-8"));

            String sResponse = reader.readLine();
            return sResponse;
        } catch (Exception e) {
            // something went wrong. connection with the server error
        }
        return null;}

I have some problem on the server side (MVC C#)

[HttpPost]
    public string SetPhoto([FromBody] string imageSource)
    {
        string bytes = imageSource;
        return "success";
    }

and this is my WebApiConfig

config.Routes.MapHttpRoute(
   name: "SetPhoto",
   routeTemplate: "api/{controller}/{imageSource}");
1

There are 1 answers

0
yogi On

You need to change this part

returnImage.Save("~/folder1/folder2"); 

To something like this

returnImage.Save("D:\\folder1\folder2\file.bmp");

Image.Save method expects a path to a file to which it needs to write the data/image to, For more info visit MSDN.