I have been using this code, but have been running into some memory issues:
// Get the image from the sdcard
Bitmap bm = BitmapFactory.decodeFile("/sdcard/myimage.jpg");
// turn image into byte array output stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 'compress' the jpeg
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
// get byte[] array of the image
byte[] byteArray = baos.toByteArray();
// turn image into base64 string
String encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
// and base64 string to 'params' value pair
params.add(new BasicNameValuePair("userfile", encodedImage));
try {
HttpPost request = new HttpPost();
String urlString = "http://www.example.com";
request.setURI(new URI(urlString));
if(params != null) {
request.setEntity(new UrlEncodedFormEntity(params));
HttpClient client = new DefaultHttpClient();
client.execute(request);
} // end if
} // end try
It has been suggested that I should use Base64OutputStream instead of Base64.encodeToString , but I have not been successful in using Base64OutputStream outputting a string that I can upload to the server. Any examples of using Base64OutputStream on an IMAGE would be a great help.
EDIT
To make the answer work, You need to add two files to your Android project: apache-mime4j-dom-0.7.2.jar and httpmime-4.1.3.jar;
You can download apache-mime4j-dom-0.7.2.jar from http://james.apache.org/download.cgi#Apache_Mime4J – download the binary, unzip it, and find the apache-mime4j-dom-0.7.2.jar file.
Then go to http://grepcode.com/snapshot/repo1.maven.org/maven2/org.apache.httpcomponents/httpmime/4.1.3 and download httpmime-4.1.3.jar
Then drag both those files into your project in Eclipse. Then in Eclipse, choose Project > Properties. Select the Properties Pop-up, select Java Build Path. Click the “Libraries” tab (Look for Source | Projects | Libraries | Order and Export). Click “Add Jars” and selectapache-mime4j-dom-0.7.2.jar and httpmime-4.1.3.jar; Then click the “Order and Export” tab. Check apache-mime4j-dom-0.7.2.jar and httpmime-4.1.3.jar; Then close that popup and choose Project > Clean from the Eclipse menu.
If possible, you should not base64encode your files and send them in URL, but use MultiPart file upload instead: