2012-03-25 32 views
3

web服務器我查看了相關的幾個問題(例如該代碼張貼了很多,但對我不起作用由於某些原因:How do I add REQUEST values to an HTTP POST method using multipart to upload a file to a PHP server in Android?),並試圖一對夫婦的解決方案,但我的文件在服務器上查看時不能解碼爲jpg。我上傳的iPhone應用程序工作,但現在當我移植到Android我想不出這樣做的正確方法。服務器端是一個asp.net流式文件上傳API。讀取JPG到安卓

這裏是我的base64編碼邏輯:

Bitmap fileToUpload = BitmapFactory.decodeFile(obs.getPhotoLocation());  
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
fileToUpload.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
byte[] image = stream.toByteArray();       
String data = Base64.encodeToString(image, true); 
new HttpConnection(handler).postBitmap(url.toString(), data); 

這裏是postBitmap代碼

HttpPost httpPostBitmap = new HttpPost(url); 
httpPostBitmap.addHeader("Content-type", "image/jpg"); 
httpPostBitmap.addHeader("Connection", "Keep-Alive"); 
httpPostBitmap.setEntity(new StringEntity(data)); 
response = httpClient.execute(httpPostBitmap); 

轉移前和服務器上的文件是相同的大小,所以我不相信這是傳輸出現問題並且更可能出現編碼問題。

這裏是工作的iOS代碼我試圖端口:

NSURL *uploadUrl = [NSURL URLWithString:FILEUPLOAD_URL]; 
UIImage *testImage = [Helpers getImageFromPhoto:self.obsToTransmit.observationPhoto]; 
NSData *imageData = UIImageJPEGRepresentation(testImage, 1.0); 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
NSString *msgLength = [NSString stringWithFormat:@"%d", [imageData length]]; 
[request addValue:@"image/jpg" forHTTPHeaderField:@"Content-type"]; 
[request setHTTPMethod:@"POST"]; 
[request addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody:imageData]; 

theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

感謝一大堆! 斯科特

+0

在這篇文章中找到答案:http://stackoverflow.com/questions/7363577/streaming-file-from-android-to-net-http-service。 – 2012-03-25 04:37:23

回答

0

嘗試使用的Base64編碼圖像以字符串和在服務器的另一端,通過解碼它的Base64的字符串改變爲圖像。檢查鏈接[http://developer.android.com/reference/android/util/Base64.html] [1]

[1]:http://developer.android.com/reference/android/util/Base64.html這是從2.2支持。我們還爲不到2.2版本的設備提供Base64.java。