2011-02-17 100 views
0

我試圖從服務器讀取圖像文件,代碼如下。它一直在進入例外狀態。我知道正確的字節數正在發送,因爲我收到時將它們打印出來。我從蟒蛇發送圖像文件像這樣Android,讀取二進制數據並將其寫入文件

#open the image file and read it into an object 
     imgfile = open (marked_image, 'rb') 
     obj = imgfile.read() 

     #get the no of bytes in the image and convert it to a string 
     bytes = str(len(obj)) 


     #send the number of bytes 
     self.conn.send(bytes + '\n') 




     if self.conn.sendall(obj) == None: 
      imgfile.flush() 
      imgfile.close() 
      print 'Image Sent' 
     else: 
      print 'Error' 

這是Android的一部分,這是我遇到問題。關於接收圖像並將其寫入文件的最佳方式的任何建議?

//read the number of bytes in the image 
     String noOfBytes = in.readLine(); 


     Toast.makeText(this, noOfBytes, 5).show(); 

     byte bytes [] = new byte [Integer.parseInt(noOfBytes)]; 

     //create a file to store the retrieved image 
     File photo = new File(Environment.getExternalStorageDirectory(), "PostKey.jpg"); 

     DataInputStream dis = new DataInputStream(link.getInputStream()); 
     try{ 


     os =new FileOutputStream(photo); 

     byte buf[]=new byte[1024]; 

     int len; 

     while((len=dis.read(buf))>0) 
     os.write(buf,0,len); 

     Toast.makeText(this, "File recieved", 5).show(); 

     os.close(); 
     dis.close(); 
     }catch(IOException e){ 

      Toast.makeText(this, "An IO Error Occured", 5).show(); 
     } 

編輯:我仍然不能得到它的工作。從那以後,我一直在這樣做,而我所有努力的結果要麼導致文件不是全尺寸,要麼導致應用程序崩潰。發送服務器端之前我知道文件沒有損壞。據我可以告訴它肯定也發送,因爲發送所有方法在python發送全部或拋出一個異常的情況下發生的錯誤,迄今爲止,它從來沒有拋出異常。所以客戶端很混亂。我必須從服務器發送文件,所以我不能使用Brian建議的建議。

+0

你有沒有得到任何數據?你得到的任何錯誤? – dongshengcn 2011-02-17 17:35:07

+0

是的,我得到的字節數,但沒有圖像數據。服務器說圖像已發送,客戶端捕獲io異常並顯示發生了IO錯誤。 – Shpongle 2011-02-17 17:39:09

回答

-1

我在Ubuntu Forums成員的幫助下解決了這個問題。這是讀取字節的問題。它正在剪切圖像中的一些字節。解決方法是隻發送整個圖像,並刪除方程中的字節發送到一起

1

從服務器獲取位圖的最佳方法是執行以下操作。

HttpClient client = new DefaultHttpClient(); 

HttpGet get = new HttpGet("http://yoururl"); 

HttpResponse response = client.execute(get); 
InputStream is = response.getEntity().getContent(); 

Bitmap image = BitmapFactory.decodeStream(is); 

這會給你你的位圖,將它保存到一個文件做類似下面的事情。

FileOutputStream fos = new FileOutputStream("yourfilename"); 
image.compress(CompressFormat.PNG, 1, fos); 
fos.close(); 

您也可以將二者結合起來,只是直接寫入到磁盤

HttpClient client = new DefaultHttpClient(); 

HttpGet get = new HttpGet("http://yoururl"); 

HttpResponse response = client.execute(get); 
InputStream is = response.getEntity().getContent(); 

FileOutputStream fos = new FileOutputStream("yourfilename"); 

byte[] buffer = new byte[256]; 
int read = is.read(buffer); 

while(read != -1){ 
    fos.write(buffer, 0, read); 
    read = is.read(buffer); 
} 

fos.close(); 
is.close(); 

希望這有助於;

0

我不確定我是否理解你的代碼。您致電dis.readFully(bytes);dis的內容寫入您的byte陣列。但是,你不會對數組做任何事情,然後嘗試通過緩衝區將dis的內容寫入您的FileOutputStream

嘗試註釋行dis.readFully(bytes);

作爲一個方面說明,我會寫的日誌,而不是彈出一個烤麪包之類的字節數,或者在發生異常:

... 
} catch (IOException e) { 
    Log.e("MyTagName","Exception caught " + e.toString()); 
    e.printStackTrace(); 
} 

你可以看看這些鏈接的例子寫一個文件到SD卡:

相關問題