2014-01-31 34 views
0

我试图将图像发送到从android.Here我们的asp.net web服务是我的示例代码:无法为Base64字符串发送时,上传图片

//获取图像从画廊

Uri selectedImage = data.getData(); 
String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
cursor.moveToFirst(); 

int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
picturePath = cursor.getString(columnIndex); 

cursor.close(); 

/* BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inSampleSize = 4;*/ 

thumbnail = (BitmapFactory.decodeFile(picturePath)); 
img_photo.setImageBitmap(thumbnail); 

//转换成IMAG的base64串

img_photo.buildDrawingCache(); 
     Bitmap bm = img_photo.getDrawingCache(); 

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     bm.compress(Bitmap.CompressFormat.PNG, 100, baos); // bm is the bitmap 

     byte[] photo = baos.toByteArray(); 
     System.out.println("this is byte array" + bytearray); 

     String temp_base =Base64.encodeToString(photo,Base64.NO_WRAP); 

//调用web服务

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

request.addProperty("CarID", SellCarDetailView.sellcardetails_carid); 
request.addProperty("pic",temp_base); 
     System.out.println("this is piccontent" +temp_base); 
try { 

      SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
        SoapEnvelope.VER11); 
soapEnvelope.encodingStyle = SoapEnvelope.ENC; 
      // new MarshalBase64().register(soapEnvelope); 
      soapEnvelope.dotNet = true; 
      soapEnvelope.setOutputSoapObject(request); 
      HttpTransportSE aht = new HttpTransportSE(URL); 
      //AndroidHttpTransport aht = new AndroidHttpTransport(URL); 
      aht.call(SOAP_ACTION, soapEnvelope); 

      // SoapObject response = (SoapObject)envelope.getResponse(); 
      SoapPrimitive response = (SoapPrimitive) soapEnvelope.getResponse(); 
      String temp3 = response.toString(); 

      Log.v("TAG", temp3); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

我在Web服务端收到“无效参数”。 // Asp.net代码

[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Xml)] 
[WebMethod(EnableSession = true)] 
public string UploadPictureByCarIDFromAndroid(string CarID, string make, string model, string year, string UserID, string pic, string AuthenticationID, string CustomerID, string SessionID) 
{ 

    string bStatus = "Failed"; 
    MobileBL objMobile = new MobileBL(); 
    UsedCarsInfo objCarPicInfo = new UsedCarsInfo(); 

    try 
    { 
        try 
      { 
       if (AuthenticationID == ConfigurationManager.AppSettings["AppleID"].ToString()) 
       { 
        objCarPicInfo.Carid = Convert.ToInt32(CarID); 
        byte[] picContent = Convert.FromBase64String(pic); 
        // byte[] picContent = Base64.decode(pic); 
        MemoryStream ms = new MemoryStream(picContent, 0,picContent.Length); // getting "invalid length" 
        ms.Write(picContent, 0, picContent.Length); 
        Bitmap oBitmap1 = new Bitmap(ms);// getting "invalid length" error here 
        // System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true); 




       } 
      } 
      catch (Exception ex) 
      { 

      } 

    } 
    catch (Exception ex) 
    { 
    } 


    return bStatus; 
} 

发送image.Any帮助时,高度赞赏我得到“长度无效”的错误。

+0

嗨,编码是题外话这里,你会更好使用的StackOverflow和标记它的 “Android” 的 “Java” 和 “ASP”。祝你好运。 – RossC

回答

0

你可以使用httpmime库上传任何文件(图像,音频,视频等..)请参考下面的代码。

HttpClient httpClient = new DefaultHttpClient(); 
HttpContext localContext = new BasicHttpContext(); 
HttpPost postRequest = new HttpPost(your url); 
MultipartEntity reqEntity = new MultipartEntity(
     HttpMultipartMode.BROWSER_COMPATIBLE); 

try { 




    File file = new File(filename); 
    FileBody fileBody = new FileBody(file); 
    reqEntity.addPart("video_file", fileBody); 

    postRequest.setEntity(reqEntity); 
    HttpResponse response = httpClient.execute(postRequest, 
      localContext); 
    HttpEntity entity = response.getEntity(); 

    if (entity != null) { 
     // entity.consumeContent(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(
       entity.getContent())); 
     // Log.e("response", "response " + rd.toString()); 
     String line; 
     String result1 = ""; 
     while ((line = rd.readLine()) != null) { 
      result1 = result1 + line; 
      // Log.e("result", "line " + line); 




} 
     // Log.e("result", "is " + result1); 
     return result1; 
    } 
} catch (Exception e) { 
    return "Exception"; 
} 
return ""; 

}

+0

谢谢@Cheerag – mamatha