-1

I am trying to send from android and get from .net page, I can get string parameter but I have difficult time getting byte array.

ANDROID

            HttpClient client = new DefaultHttpClient();

            HttpPost post = new HttpPost(urls[0]);

            MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
            entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

            entityBuilder.addTextBody("X", "Y");

            File photo= new File(Environment.getExternalStorageDirectory(), "photo.jpg");

            if(photo != null)
            {
                entityBuilder.addBinaryBody("IMAGE", photo);
            }

            HttpEntity entity = entityBuilder.build();
            post.setEntity(entity);
            HttpResponse response = client.execute(post);
            HttpEntity httpEntity = response.getEntity();
            String result = EntityUtils.toString(httpEntity);

.NET

        byte[] bitmap = (byte[])Request.Params["IMAGE"];

        using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(bitmap)))
        {
            image.Save(@"C:/output.jpg", ImageFormat.Jpeg);
        }

TRY 1

        byte[] bitmap = GetBytes(Request.Form["IMAGE"]);

        Log("HERE");

        using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(bitmap)))
        {
            Log("Save 1");
            image.Save(@"C:/doka/output.jpg", ImageFormat.Jpeg);  // Or Png
            Log("Save 2");
        }
    }

    static byte[] GetBytes(string str)
    {
        Log("GetBytes 1");

        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        Log("GetBytes 2");

        return bytes;
    }
Mert
  • 5,728
  • 6
  • 28
  • 60
  • the error message tells you exactly what the problem is .. do you know the difference between `string and byte[]` or even string and this symbol `[]` one is an array of something the other is not.. also you have the source code at your finger tips.. so `Use the DEBUGGER!!` – MethodMan Feb 11 '15 at 19:19
  • Where is your exception? Is it here: `(byte[])Request.Params["IMAGE"];`? If so check this: http://stackoverflow.com/questions/472906/converting-a-string-to-byte-array-without-using-an-encoding-byte-by-byte – Nikolay Kostov Feb 11 '15 at 19:19
  • You write the image to body (`addBinaryBody`) and try to read from url (`Request.Params`) (BTW: [Fiddler](http://www.telerik.com/fiddler) is a very good tool when developing web apps ) – EZI Feb 11 '15 at 19:21
  • 1
    This is actually already been answered here : http://stackoverflow.com/questions/472906/converting-a-string-to-byte-array-without-using-an-encoding-byte-by-byte – Philip Stuyck Feb 11 '15 at 19:25
  • Philip I tried your solution but I think It cant convert to Image, as you see at top I can get "GetBytes 1" , "GetBytes 2", "HERE" but no log after that "Parameter is not valid." exception. – Mert Feb 11 '15 at 19:43
  • Mert, have you read my comment? Your problem is not related with string/byte[] conversion. You can not get your image like `Request.Params["IMAGE"]` – EZI Feb 11 '15 at 19:55
  • why is that? when I write bytes to text file all is there. – Mert Feb 11 '15 at 19:57
  • Mert Please read some docs about HTTP protocol. It has a *header* and a *body* or *content*. You have different methods to read/write different parts of an HTTP request/response. – EZI Feb 11 '15 at 19:59
  • what do you suggest about sending image to .net web app? – Mert Feb 11 '15 at 20:00
  • 1
    Mert at first glance, it seems your sending code is OK(I am not an Android developer). just forget `Request.Params` and search how you can read the **content** of the request. – EZI Feb 11 '15 at 20:01
  • EZI, I followed your guidance and looked for another way around to send the image and found, thanks for your contribution. – Mert Feb 11 '15 at 20:28

1 Answers1

0

This is how I could sloved the issue, If someone else also need;

ANDRIOD

            File photo = new File(Environment.getExternalStorageDirectory(), "photo.jpg");

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bitmap = BitmapFactory.decodeFile(photo.getAbsolutePath(), options);
            //selected_photo.setImageBitmap(bitmap);

               HttpClient httpClient = new DefaultHttpClient();
               HttpPost post = new HttpPost(urls[0]);

                MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
                entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

                try{
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    bitmap.compress(CompressFormat.JPEG, 75, bos);
                    byte[] data = bos.toByteArray();
                    ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
                    entityBuilder.addPart("IMAGE", bab);
                }
                catch(Exception e){
                    //Log.v("Exception in Image", ""+e);
                }

                HttpEntity entity = entityBuilder.build();
                post.setEntity(entity);   
                HttpResponse response = null;
                try {
                    response = httpClient.execute(post);
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                BufferedReader reader = null;
                try {
                    reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                } catch (UnsupportedEncodingException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IllegalStateException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                String sResponse;
                StringBuilder s = new StringBuilder();
                try {
                    while ((sResponse = reader.readLine()) != null) {
                        s = s.append(sResponse);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

.NET

        HttpFileCollection files = Request.Files;

        HttpPostedFile file = files[0];
        int filelength = file.ContentLength;
        byte[] input = new byte[filelength];
        file.InputStream.Read(input, 0, filelength);
        file.SaveAs(@"C:/doka/output.jpg");
Mert
  • 5,728
  • 6
  • 28
  • 60