Wednesday, 11 September 2013

OutOfMemoryError when sending image from android to php server

OutOfMemoryError when sending image from android to php server

Im triing to send image from my sdcard to php server ... I create 2 class
: the first is MainActivity.java and the second is Base64.java which
encodeBytes in Base64 Format.
My MainActivity.java is:
public class MainActivity extends Activity {
InputStream inputStream;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
Bitmap bitmap =
BitmapFactory.decodeFile("/sdcard/DCIM/Camera/1378889572299.jpg");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress
to which format you want.
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",image_str));
Log.e("ok", "1");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://myserver.com/upload_image.php");
Log.e("ok", "3");
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.e("ok", "4");
HttpResponse response = httpclient.execute(httppost);
Log.e("ok", "5");
String the_string_response = convertResponseToString(response);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String convertResponseToString(HttpResponse response) throws
IllegalStateException, IOException{
String res = "";
StringBuffer buffer = new StringBuffer();
inputStream = response.getEntity().getContent();
final int contentLength = (int)
response.getEntity().getContentLength(); //getting content
length…..
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "contentLength : " +
contentLength, Toast.LENGTH_LONG).show();
}
});
if (contentLength < 0){
}
else{
byte[] data = new byte[512];
int len = 0;
try
{
while (-1 != (len = inputStream.read(data)) )
{
buffer.append(new String(data, 0, len));
//converting to string and appending to
stringbuffer…..
}
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
inputStream.close(); // closing the stream…..
}
catch (IOException e)
{
e.printStackTrace();
}
res = buffer.toString(); // converting stringbuffer to
string…..
final String res2 = res;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Result : " + res2,
Toast.LENGTH_LONG).show();
}
});
//System.out.println("Response => " +
EntityUtils.toString(response.getEntity()));
}
return res;
}
}
I have this erreur :
........OutOfMemoryError
….at java.lang.AbstractStringBuilder.(AbstractStringBuilder.java:81)
….at java.lang.StringBuilder.(StringBuilder.java:68)
….at java.net.URLEncoder.encode(URLEncoder.java:98)
….at
org.apache.http.client.utils.URLEncodedUtils.encode(URLEncodedUtils.java:184)
….at
org.apache.http.client.utils.URLEncodedUtils.format(URLEncodedUtils.java:163)
….at
org.apache.http.client.entity.UrlEncodedFormEntity.(UrlEncodedFormEntity.java:71)
….at
com.example.imageuploadonserver.MainActivity.onCreate(MainActivity.java:43)
line 43 is: httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
I make some change in my code ... and i replace the image source from my
sdcard to my drawabel.ic_luncher :
Bitmap bitmap =
BitmapFactory.decodeFile("/sdcard/DCIM/Camera/1378889572299.jpg");
by this :
Bitmap bitmap =
BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
and it work succseffuly !!!!
I googled a lot, but either I used the wrong keywords or there are no
simple solutions on the internet. I hope somebody here can help me.
Best regards and thanks in advance, Fadel.

No comments:

Post a Comment