Getting bitmap from server?

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

Getting bitmap from server?

Postby Damonh » Thu Feb 18, 2010 11:31 am

Hello!

I'm rather unexperienced when it comes to servers and server connections, so it's likely my problem lies there in some way.

Anyway, what I want to do is load an image from a server. I've created a local server on my comp. from which I can successfully read some text. The server uses a socket connection by

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. serversocket = new ServerSocket(port);
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


and then just listens for clients. As I mentioned it works well to send some text. The text is loaded from a file in a local folder and then eventually sent by:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.       while(sendstring!=null){
  2.  
  3.                   outputin.writeBytes(sendstring+"\n");
  4.  
  5.                   sendstring = br.readLine();
  6.  
  7.       }
Parsed in 0.010 seconds, using GeSHi 1.0.8.4

(br.readLine() just reads from the mentioned local file).

So, all this works! But sending a picture does not. Here's my code for the picture sending method:



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.   public void SendPicture(Socket conn, InetAddress clientin){
  3.  
  4.          
  5.  
  6.             FileInputStream requestedfile = null;
  7.  
  8.  
  9.  
  10.             try {
  11.  
  12.                
  13.  
  14.                         String substringtemp = inputfromclient.substring(inputfromclient.indexOf("&")+1);                      
  15.  
  16.                         substringtemp = substringtemp.substring(substringtemp.indexOf("&")+1);
  17.  
  18.                         String filename = substringtemp.substring(substringtemp.indexOf("&")+1);
  19.  
  20.  
  21.  
  22.                         System.out.println("sending picture... "+filename);
  23.  
  24.                
  25.  
  26.                         requestedfile = new FileInputStream("Graphics/"+filename);
  27.  
  28.  
  29.  
  30.                 DataOutputStream output =
  31.  
  32.                     new DataOutputStream(conn.getOutputStream());
  33.  
  34.                
  35.  
  36.         while (true) {
  37.  
  38.             //read the file from filestream, and print out through the
  39.  
  40.             //client-outputstream on a byte per byte base.
  41.  
  42.             int b;
  43.  
  44.            
  45.  
  46.                         try {
  47.  
  48.                                 b = requestedfile.read();
  49.  
  50.                     if (b == -1) {
  51.  
  52.                         break; //end of file
  53.  
  54.                       }
  55.  
  56.                     //Send byte
  57.  
  58.                     output.write(b);
  59.  
  60.                                    
  61.  
  62.                         } catch (IOException e) {
  63.  
  64.                                 // TODO Auto-generated catch block
  65.  
  66.                                 e.printStackTrace();
  67.  
  68.                         }
  69.  
  70.  
  71.  
  72.       }
  73.  
  74.         output.close();
  75.  
  76.        
  77.  
  78.                 }
  79.  
  80.             catch (FileNotFoundException e) {
  81.  
  82.                         // TODO Auto-generated catch block
  83.  
  84.                         e.printStackTrace();
  85.  
  86.                 }
  87.  
  88.             catch (IOException e) {
  89.  
  90.                         // TODO Auto-generated catch block
  91.  
  92.                         e.printStackTrace();
  93.  
  94.                 }
  95.  
  96.           System.out.print("OK!");
  97.  
  98.   }
  99.  
  100.  
Parsed in 0.013 seconds, using GeSHi 1.0.8.4




In my android app (the client). I try to read the picture by:



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void FetchImage(){
  2.  
  3.                
  4.  
  5.                 String myURL = "http://xx.xxx.xxx.xxx"; //Just hiding the IP <img src="http://www.anddev.org/images/smilies/wink.png" alt=";)" title="Wink" />
  6.  
  7.  
  8.  
  9.                 URL url = null;
  10.  
  11.                 URLConnection conn;
  12.  
  13.                        
  14.  
  15.                 try{
  16.  
  17.                         url = new URL(myURL);
  18.  
  19.                 }
  20.  
  21.                 catch(Exception e){    
  22.  
  23.                 }
  24.  
  25.                
  26.  
  27.                 try{
  28.  
  29.                
  30.  
  31.                 conn = url.openConnection();
  32.  
  33.                 conn.setDoOutput(true);
  34.  
  35.                 conn.setUseCaches(false);
  36.  
  37.                 conn.setDoInput(true);
  38.  
  39.                
  40.  
  41.                 BufferedWriter bwriter = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
  42.  
  43.                 bwriter.write("Username&password&getImage&testpic.png");
  44.  
  45.                 bwriter.flush();
  46.  
  47.  
  48.  
  49.                 tempbitmap = DownloadImage(conn);
  50.  
  51.                
  52.  
  53.                 }
  54.  
  55.                 catch(Exception e){    
  56.  
  57.                 }
  58.  
  59.                
  60.  
  61.         }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.     public Bitmap DownloadImage(URLConnection connection)
  68.  
  69.     {        
  70.  
  71.         Bitmap bitmap = null;
  72.  
  73.         BufferedInputStream in = null;      
  74.  
  75.  
  76.  
  77.         try {
  78.  
  79.                 in = new BufferedInputStream(connection.getInputStream());
  80.  
  81.  
  82.  
  83.             bitmap = BitmapFactory.decodeStream(in);
  84.  
  85.  
  86.  
  87.             in.close();
  88.  
  89.         } catch (IOException e1) {
  90.  
  91.             // TODO Auto-generated catch block
  92.  
  93.             e1.printStackTrace();
  94.  
  95.         }
  96.  
  97.         return bitmap;                
  98.  
  99.     }
Parsed in 0.013 seconds, using GeSHi 1.0.8.4



But the bitmap is always returned as null, which I guess means the stream is not valid - because there is in fact an existing stream.

However, if I change FetchImage, so that I don't call my local server but instead use e.g. an URL I found in another thread on anddev as below to a picture on a web page; Then the bitmap is loaded properly. That code below:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void FetchImage(){
  2.  
  3.                
  4.  
  5.                 String myURL = "http://www.anddev.org/images/tiny_tutheaders/weather_forecast.png";
  6.  
  7.  
  8.  
  9.                 URL url = null;
  10.  
  11.                 URLConnection conn;
  12.  
  13.                        
  14.  
  15.                 try{
  16.  
  17.                         url = new URL(myURL);
  18.  
  19.                 }
  20.  
  21.                 catch(Exception e){    
  22.  
  23.                 }
  24.  
  25.                
  26.  
  27.                 try{
  28.  
  29.                
  30.  
  31.                 conn = url.openConnection();
  32.  
  33.                 conn.setUseCaches(false);
  34.  
  35.                 conn.setDoInput(true);
  36.  
  37.  
  38.  
  39.  
  40.  
  41.                 tempbitmap = DownloadImage(conn);
  42.  
  43.                
  44.  
  45.                 }
  46.  
  47.                 catch(Exception e){    
  48.  
  49.                 }
  50.  
  51.                
  52.  
  53.         }
  54.  
  55.  
Parsed in 0.011 seconds, using GeSHi 1.0.8.4



Any idea why reading the image from the local server doesn't work? Any suggestions on how I could make it work? Or is it perhaps a dumb idea to start with? :P

Thanks in advance for any replies:)

Regards,
Damonh
Damonh
Junior Developer
Junior Developer
 
Posts: 18
Joined: Thu Jan 21, 2010 3:56 pm

Top

Postby mrco » Fri Feb 19, 2010 3:06 pm

How your android device are connected to network? WIFI or mobile provider? Are you shure that device can really connect to your local server. Check firewalls etc.
mrco
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Jan 15, 2010 11:31 am
Location: 2V Software

Postby vasanth62 » Sun Feb 28, 2010 10:10 am

Are you sure the text send/receive is working fine? Looking at your code, it doesn't look like that!

When you are connecting to a URL, you are basically making use of the http protocol and connecting to port 80. But, in your server, you've opened a socket with the call:

serversocket = new ServerSocket(port)

So you'll need to connect to this port and retrieve the information.

Search for socket programming and you should be good to go!
vasanth62
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Feb 28, 2010 10:04 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 5 guests