1

I am trying to upload an image from my device file system to a server. The upload works fine when i use a WIFI connection, but fails when on GPRS. My code is as below:

String request=null;
byte[] attachmentData;
//read the image from the file system
attachmentData=bytesReadfromthefilesystem; 
//I use Apache's Base64 encoding to convert the byte array to string    
 request=Base64.encode(data);

  URL url = new URL(
        "http://mydomain.com:9090//abc/http?ID=12345");
        HttpURLConnection  httpURLConnection = (HttpURLConnection) url
            .openConnection();
    httpURLConnection.setRequestProperty("Content-Type",
            "text/plain");
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setChunkedStreamingMode(0);
    httpURLConnection.connect();
    OutputStream outStream outStream =httpURLConnection.getOutputStream();
    if (outStream != null) {
            if (request.getData().length() > 0) {
                outStream.write(request.getBytes());
            }

            outStream.flush();
            outStream.close();
            outStream = null;      
      }

The image size is close to 1MB. I am trying on Samsung Galaxy Pop (Android 2.2.1). I do not get any errors too. Am i missing out on something here? Could someone kindly help me with this? Thanks in advance.

S.A.Norton Stanley
  • 1,833
  • 3
  • 23
  • 37

2 Answers2

1

did you set the permission on your AndroidManifest file?

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
Alysson Myller
  • 1,203
  • 11
  • 13
-1

Try to setConnectionTimeout() to 300000 (5 minutes) or something like that.

Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
  • I have set the time out to 5 minutes but still seems to be the same. Is there any other better way of doing this? – S.A.Norton Stanley Jul 27 '11 at 11:39
  • You should try to make a MultiPartPost. Check this SO [thread](http://stackoverflow.com/questions/3038409/how-to-send-http-post-request-and-recieve-response/3038747#3038747) of how to do this. – Ovidiu Latcu Jul 27 '11 at 12:08
  • Using a small image around 200 Kb gets uploaded using GPRS – S.A.Norton Stanley Jul 27 '11 at 12:49
  • It must be the connection then... maybe you lose your connection or something like that. Check if you get any answer from the connection... I'm not sure how you can do that using your techique. – Ovidiu Latcu Jul 27 '11 at 12:55