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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to