Note that there are some explanatory texts on larger screens.

plurals
  1. POOutOfMemoryError when sending image from android to php server
    primarykey
    data
    text
    <p>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.</p> <p>My MainActivity.java is:</p> <pre><code>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&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(); 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 &lt; 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 =&gt; " + EntityUtils.toString(response.getEntity())); } return res; } } </code></pre> <p>I have this erreur :</p> <pre><code>........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) </code></pre> <p>line 43 is: httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));</p> <p>I make some change in my code ... and i replace the image source from my sdcard to my drawabel.ic_luncher :</p> <pre><code>Bitmap bitmap = BitmapFactory.decodeFile(“/sdcard/DCIM/Camera/1378889572299.jpg”); </code></pre> <p>by this :</p> <pre><code>Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher); </code></pre> <p>and it work succseffuly !!!!</p> <p>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.</p> <p>Best regards and thanks in advance, Fadel.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload