2015-02-11 67 views
-1

我想從android發送並從.net頁面,我可以得到字符串參數,但我有困難的時間獲取字節數組。不能轉換類型'字符串'字節[]'

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; 
    } 
+0

錯誤消息告訴你到底是什麼問題..你知道字符串和字節[]'甚至字符串,這之間的'差異符號'[]'一個是另一個不是的數組..你也有你的指尖的源代碼..所以'使用DEBUGGER !!' – MethodMan 2015-02-11 19:19:38

+0

你的例外在哪裏?它在這裏:'(byte [])Request.Params [「IMAGE」];'?如果是這樣檢查:http://stackoverflow.com/questions/472906/converting-a-string-to-byte-array-without-using-an-encoding-byte-by-byte – 2015-02-11 19:19:41

+0

您將圖像寫入body( 'addBinaryBody')並嘗試從url('Request.Params')中讀取(在開發Web應用程序時,[Fiddler](http://www.telerik.com/fiddler)是一個非常好的工具) – EZI 2015-02-11 19:21:03

回答

0

這是我怎麼能sloved的問題,如果其他人也需要;

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"); 
相關問題