Base64 Encodebytes - Out Of Memory Exception
Solution 1:
I had same problem before for large images...I have just scaled bitmap before converting it to Base64... you can use it as following...
bitmap=Bitmap.createScaledBitmap(bitmap, 100, 100, true);
ByteArrayOutputStreamstream=newByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); //compress to which format you want.byte [] byte_arr = stream.toByteArray();
Stringimage_str= Base64.encodeBytes(byte_arr);
Solution 2:
Based on these statements:
1- I ma trying to upload a picture from android to remote server.
2- but its scaling the bimap rite ?? and i want to upload it in its original image.
I'm going to assume any answer will do as long as the solution helps you upload files to a server from android application and keeps the original image.
Solution - Remove the compression out of the tutorial below and it should send you on your way.
http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/
Avoid Base64.encodeBytes() for large image files.
Solution 3:
I found the solution regarding uploading exact original size of image to server:
publicstaticvoidupload(String pathToOurFile, String serverUrl){
HttpURLConnectionconnection=null;
DataOutputStreamoutputStream=null;
DataInputStreaminputStream=null;
StringurlServer= serverUrl;
StringlineEnd="\r\n";
StringtwoHyphens="--";
Stringboundary="*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
intmaxBufferSize=1*1024*1024;
try
{
FileInputStreamfileInputStream=newFileInputStream(newFile(pathToOurFile) );
URLurl=newURL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outputStream = newDataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = newbyte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)intserverResponseCode= connection.getResponseCode();
StringserverResponseMessage= connection.getResponseMessage();
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
//Exception handling
}
}
Solution 4:
you can try to change the quality parameter of compress like i have just given to 10 & it will work fine for large image also.
ByteArrayOutputStreamstream=newByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 10, stream);
byte [] byte_arr = stream.toByteArray();
Stringimage_str= Base64.encodeBytes(byte_arr);
Post a Comment for "Base64 Encodebytes - Out Of Memory Exception"