It looks like you're sending a POST request for every line you're
encoding.

If you're trying to do it in one shot, then why have a while
statement?

In any case, do not do it in one shot, use buffered streams instead,
it could happen that your image is too big,
so you're better off reading that FileInputStream wrapped with a
BufferedInputStream,
at the same time, prepare your HTTP connection, then, as you read from
the bufferedInputStream, do your base64 encoding,
and then write to a buffered output stream (to your HTTP) connection.

Also, you should probably send the name of the file as an HTTP Header
that you could parse on your server side.

As I write this I'm also thinking that it might be useful on your
server side what the file size will be. If I remember correctly
everything in base64 is padded (every line should be 64 bytes), maybe
you can do some math to know how big will be the
encoded image in advance and send the size on a Content-Length http
header. If not you can do the the conversion,
save the base64 version of the image on a temporary file, and then
find out the resulting size, then just send that file as you
would post a regular file.

On Apr 27, 1:44 am, "pramod.deore" <deore.pramo...@gmail.com> wrote:
> hello I want to convert a image into Base64 format and send it to
> server. I have one Base64 class which accept byte array and return a
> string.I am sending image into chunks of data.I am creating creating
> one chunk of  100kb. If image is less than 100 kb then image is
> uploaded on server, but if it is greater than 100kb (i.e when it
> requires more than one chunk) then it will not uploaded. I think it is
> because I am converting image into Base64 in parts and on server it
> decode it only in one shot. Now I want to send a image for conversion
> of Base64 only in one chunk. But if image is too large then it throw
> OutOfMemoryException (Obviously after all it is mobile having very
> limeted resources).
>
> My code is as follows
>
> import java.io.File;
> import java.io.FileInputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.io.OutputStream;
> import java.net.HttpURLConnection;
> import java.net.MalformedURLException;
> import java.net.URL;
>
> import android.app.Activity;
> import android.os.Bundle;
>
> public class MicroFinalImageUp1 extends Activity
> {
>
>         File myFile = new File("/sdcard/Images/4.jpg");
>         int noOfChunks ;
>         String base64String,str;
>     /** Called when the activity is first created. */
>     @Override
>     public void onCreate(Bundle savedInstanceState)
>     {
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.main);
>          readFile();
>
>     }
>   public void readFile()
>     {
>          try
>          {
>                  int size = (int)myFile.length();
>                  noOfChunks = (size/102400);
>                  noOfChunks = noOfChunks+1;
>                  System.out.println ("size of file is "+size);
>                  System.out.println ("size is"+noOfChunks);
>
>                  byte[] byteArray = new byte[102400];
>                  FileInputStream fstream = new FileInputStream(myFile);
>                  int bytesRead = 0;
>                  while((bytesRead = fstream.read(byteArray)) != -1)
>                  {
>                          str = new String(byteArray,0,bytesRead);
>
>                          base64String = Base64.encode(byteArray);
>
>                          
> sendData("0213456789_P~myimage.jpg~10~"+base64String);
>                  }
>
>          }
>          catch (IOException ioe)
>          {
>                  ioe.printStackTrace();
>          }
>
>     }
>
>     public void sendData(String line)
>     {
>         try
>         {
>
>                 URL connectURL = new URL("MyURL");
>                 // connectURL is a URL object
>                 System.out.println ("After heating url");
>
>                 HttpURLConnection conn =
> (HttpURLConnection)connectURL.openConnection();
>                 // allow inputs
>                 conn.setDoInput(true);
>
>                 // allow outputs
>                 conn.setDoOutput(true);
>
>                 // don't use a cached copy
>                 conn.setUseCaches(false);
>
>                 // use a post method
>                 conn.setRequestMethod("POST");
>
>                 // set post headers
>                 conn.setRequestProperty("Connection","Keep-Alive");
>
>                 // open data output stream
>                 OutputStream dos ;
>
>                 dos=conn.getOutputStream();
>
>                 System.out.println ("Before if statement");
>
>                         byte[] arr = line.getBytes();
>
>                         System.out.println ("arr auccesfully 
> created"+arr.length);
>
>                         dos.write(arr);
>
>                         System.out.println ("write to the page");
>
>                 System.out.println ("After if statement");
>                 dos.flush();
>
>                 InputStream is = conn.getInputStream();
>                 int ch;
>                 StringBuffer b =new StringBuffer();
>                 System.out.println ("Before second while loop");
>                 while(( ch = is.read() ) != -1 )
>                 {
>                         b.append( (char)ch);
>
>                 }
>                 String s=b.toString();
>                 System.out.println (s);
>                 dos.close();
>                 System.out.println ("at the end of try block");
>
>         }
>         catch (MalformedURLException ex)
>         {
>                 // Log.e(Tag, "error: " + ex.getMessage(), ex);
>         }
>         catch (IOException ioe)
>         {
>                 // Log.e(Tag, "error: " + ioe.getMessage(), ioe);
>         }
>
>     }
>
> }
>
> What should I do?
>
> Thanks in advance
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group 
> athttp://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to