0

Im trying to read in image file from a server , with the code below . It keeps going into the exception. I know the correct number of bytes are being sent as I print them out when received. Im sending the image file from python like so

#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'

Here is the android part , this is where I'm having the problem. Any suggestions on the best way to go about receiving the image and writing it to a file ?

//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();
       }

EDIT: I still cant seem to get it working. I have been at it since and the result of all my efforts have either resulted in a file that is not the full size or else the app crashing. I know the file is not corrupt before sending server side. As far as I can tell its definitely sending too as the send all method in python sends all or throws an exception in the event of an error and so far it has never thrown an exception. So the client side is messed up . I have to send the file from the server so I cant use the suggestion suggested by Brian .

Shpongle
  • 983
  • 1
  • 9
  • 14

3 Answers3

1

The best way to get a bitmap from a server is to execute the following.

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);

That will give you your bitmap, to save it to a file do something like the following.

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

You can also combine the two and just write straight to disk

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();

Hope this helps;

Brian Griffey
  • 4,751
  • 1
  • 25
  • 26
0

I'm not sure I understand your code. You are calling dis.readFully(bytes); to write the content of dis into your byte array. But then you don't do anything with the array, and then try to write the content of dis through a buffer into your FileOutputStream.

Try commenting out the line dis.readFully(bytes);.

As a side note, I would write to the log rather than popping up a toast for things like the number of bytes or when an exception occurs:

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

You could look at these links for examples of writing a file to the SD card:

Community
  • 1
  • 1
dave.c
  • 10,910
  • 5
  • 39
  • 62
  • Yes your right, my mistake I'm new to networking . After commenting that out The error is gone and the file is created but shows up empty as for the toast its just to test it quickly , It wont be in the actual app – Shpongle Feb 17 '11 at 18:00
  • @Shpongle You should definitely be logging errors while you are testing the app. It will help you to find and fix issues like this more quickly. – dave.c Feb 17 '11 at 18:05
  • Thanks for the advice Dave , I will add logs in. Ok the file is writing but there is not enough data sending , Ill have to look at the buffer size – Shpongle Feb 17 '11 at 18:22
  • I can't seem to get it to work. Even if I have tried setting the buffer to the exact file size , tired checking if the number of bytes is odd or even and have a buffer size of bytes/4 if even or bytes/4 + 1 followed by 3 iterations of bytes/4 to make up the total odd number. I have also tried a buffer of one (just to see). – Shpongle Feb 19 '11 at 15:11
-1

I solved it with the help of a Ubuntu Forums member. It was the reading of the bytes that was the problem . It was cutting some of the bytes from the image. The solution was to just send the image whole and remove the sending of the bytes from the equation all together

Shpongle
  • 983
  • 1
  • 9
  • 14