OutputStream: prblm w/ transmission when not using .close()

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

OutputStream: prblm w/ transmission when not using .close()

Postby gymshoe » Tue Nov 25, 2008 3:04 am

I am sending jpg images via TCP, using InputStream and OutputStream:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                 //  Note that valid outStream and inStream variables have been established and passed to the
  3.  
  4.                 //  picTransmit method
  5.  
  6.  
  7.  
  8.                 public picTransmit(Bitmap sendBitmap, OutputStream outStream,
  9.  
  10.                                 InputStream inStream) {  
  11.  
  12.                 // First, send the sendBitmap
  13.  
  14.                         sendBitmap.compress(Bitmap.CompressFormat.JPEG, 80, outStream);
  15.  
  16.                         try {
  17.  
  18.                                 outStream.close();
  19.  
  20.                         } catch (IOException e) {
  21.  
  22.                                 e.printStackTrace();
  23.  
  24.                         }
  25.  
  26.  
  27.  
  28.                 // Second, receive an incoming Bitmap
  29.  
  30.                         Bitmap receiveBitmap = BitmapFactory.decodeStream(inStream);
  31.  
  32.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4



I can send a picture, but when I try to receive a picture, I get a "Bad Socket" error.
Based on the following:
http://www.mail-archive.com/android-dev ... 02516.html
I figured that my problem was that invoking the outputStream.close() after the first image was sent was somehow interfering with my preestablished inputStream. (If I didn't use outputStream.close() then the first image transmission never concludes properly.) I don't really understand why this is so [Any ideas?], but anyway I tried to use the outputStream.flush() (per the above html) but when I did this (without using outputStream.close()), the picture is never transmitted.

In looking at the Android API for OutputStream (http://code.google.com/android/referenc ... tream.html), I found several interesting issues.
1) "OutputStream is an abstract class..." : Well if that is so, how is that I was able to instantiate concrete variables of type OutputStream? OK, maybe they just never updated the API documentation from earlier??? Don't know...
2) regarding the android documentation of the flush() method:
"public void flush()
Flush this OutputStream. Concrete implementations of this class should ensure any pending writes to the underlying stream are written out when this method is envoked. This implementation does nothing. "

Is this why the .flush() method is not working? Although, the .close() method does seem to work to finalize sending the image, and the documentation for the .close() method in the API also says: "This implementation does nothing. ".

So, The question is : Can anybody guide me in where to find out how to write the code that the Android API recommends to "ensure any pending writes to the underlying stream are written out when this method [.flush()] is envoked"? Or give any other insight?

Thanks!
JIm
gymshoe
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Sep 10, 2008 6:07 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 2 guests