2012-02-22 77 views
0

我試圖使用webservice獲取圖像並保存到SD卡。該文件保存但我無法打開文件。一旦我打開文件它說「無法加載圖像」。以下是我的代碼。文件沒有正確保存

httpTransport.call(SOAP_ACTION, envelope); 
Object response = envelope.getResponse(); 
test = response.toString(); 
Blob picture = org.hibernate.Hibernate.createBlob(test.replaceAll("-", "").getBytes()); 
String FILENAME = "voucher1.jpg"; 
File root = Environment.getExternalStorageDirectory(); 
FileOutputStream f = new FileOutputStream(new File(root, FILENAME)); 
InputStream x=picture.getBinaryStream(); 
int size=x.available(); 
byte b[]= new byte[size]; 
x.read(b); 
f.write(b); 
f.close(); 

請幫忙。謝謝

+0

測試內容是什麼,爲什麼要用''替換' - '? – njzk2 2012-02-22 09:43:41

+0

它是一個字節[],我將其轉換爲webservice中的字符串並在此處返回值。我替換' - '的原因是因爲當我檢查測試字符串..默認情況下是' - '... – 2012-02-22 09:46:41

+0

這可能是一個問題。 byte [] - > String - > byte []不起作用,如果您使用不同的編碼,我很可能。另外,創建Blob的意義何在?如果你刪除所有的連字符,你會丟失文件的一部分 – njzk2 2012-02-22 13:37:47

回答

0

我改變了format..instead使用Web服務,我只是用圖像的URL檢索圖像和它的作品...

我嘗試這和它的做工精細。謝謝。

URL url = new URL(fileURL); 
URLConnection conexion = url.openConnection(); 
conexion.connect(); 
int lenghtOfFile = conexion.getContentLength(); 
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 
InputStream input = new BufferedInputStream(url.openStream()); 
OutputStream output = new FileOutputStream("/sdcard/caldophilus.jpg"); 
byte data[] = new byte[1024]; 
long total = 0; 
while ((count = input.read(data)) != -1) { 
total += count; 
output.write(data, 0, count); 
} 
output.flush(); 
output.close(); 
input.close(); 
-1

我假設你需要調用f.flush()爲了寫出流中的所有數據到文件。

f.flush(); 
f.close(); 
+0

nope ...相同的問題.. – 2012-02-22 09:56:46

+0

文檔:「這個流沒有被緩衝,大多數調用者應該用BufferedOutputStream包裝這個流。無論如何,關閉一個流足以導致一切都被刷新 – colithium 2012-02-23 05:47:57