无法转换类型'字符串'到' byte []'

时间:2015-02-11 19:17:42

标签: c# android .net

我正在尝试从android发送并从.net页面获取,我可以获取字符串参数但是我很难获得字节数组。

机器人

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

尝试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;
    }

1 个答案:

答案 0 :(得分:0)

这就是我解决问题的方法,如果其他人也需要;

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