2012-02-22 80 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