Hello everybody, I am developing an application in that I am reading
image from mobile and send it to server.If size of image is less than
700kb then it is uploaded on server sucessfully, but for big size
images it throws OutOfMemoryException. So now I want to send image in
parts to server. How should I do this?
I want to read image convert it to Base64 format and send
it to server.I have Base 64 class which accept byte array and return
string. On server side image is decoded from Base64 directly in one
part(in one shot).
So I think I had to create ont txt file and read image in
parts say 100kb , convert it to Base64 format and write that string to
txt file and after reading whole file I had to send data from that
txt file to server in parts. I had tried to write the code but it is
not work properly. My code is like
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
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/c.jpeg");
File myFile1 = new File("/sdcard/Images/micro.txt");
PrintWriter pw ;
int noOfChunks ;
String base64String,str;
boolean test = true;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try
{
myFile1.createNewFile();
pw = new PrintWriter(myFile1);
}
catch (Exception e)
{
e.printStackTrace();
}
sendData("0213456789_P~myimage.jpg~START~10"); // This line I
have to send every time before sending actual data
readFile();
pw.flush();
pw.close();
sendData("0213456789_P~myimage.jpg~END~10"); //After sending
actual data I have to sent this
}
public void readFile()
{
try
{
byte[] byteArray = new byte[102400];
FileInputStream fstream = new FileInputStream(myFile);
int bytesRead = 0;
while((bytesRead = fstream.read(byteArray)) != -1)
{
pw = new PrintWriter(myFile1);
str = new String(byteArray,0,bytesRead);
base64String = Base64.encode(byteArray);
writeDataToFile();
System.out.println("**************"+base64String);
sendData("0213456789_P~myimage.jpg~10~"+base64String);
base64String=null;
pw.flush();
pw.close();
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
public void writeDataToFile()
{
try
{
pw.write(base64String);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void sendData(String line)
{
try
{
URL connectURL = new URL("My URL");
// 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);
}
}
}
But this code not working properly, It only write first 100kb of data.
Actually I want to read image convert it to base 64 and write that
converted image to a file how to do that?
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