0

I am uploading a picture taken by user in Android to my shared hosting using PHP post request. I keep the file path to the picture so I can decode it as Bitmap and then send it to the server.

EDIT: It seems that the issue is coming for the size of the pictures, if I try to upload a photo with size exceeding 800 KB I am getting the following error from the server:

    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>413 Request Entity Too Large</title>
</head><body>
<h1>Request Entity Too Large</h1>
The requested resource<br />/upload_image.php<br />
does not allow request data with GET requests, or the amount of data provided in
the request exceeds the capacity limit.
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>

The Android method:

private void uploadImage() {
        Bitmap bitmap = BitmapFactory.decodeFile(CURRENT_PHOTO_PATH);
        //Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
        Log.d("Bitmap processing", "DONE!");
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);

        byte[] byte_arr = stream.toByteArray();
        String image_str = Base64.encodeBytes(byte_arr);
        final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("image", image_str));
        nameValuePairs.add(new BasicNameValuePair("event_id", Integer
                .toString(0)));

        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(url);
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    final String the_string_response = convertResponseToString(response);
                    Log.d("Image processing", "UPLOADED!");
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Toast.makeText(UploadImage.this,
                                    "Ready " + the_string_response,
                                    Toast.LENGTH_LONG).show();
                        }
                    });

                } catch (final Exception e) {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Toast.makeText(UploadImage.this,
                                    "ERROR " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }
                    });
                    System.out.println("Error in http connection "
                            + e.toString());
                }
            }
        });
        t.start();

    }

I wonder how I can solve this problem? Thank you in advance!

Georgi
  • 684
  • 7
  • 21
  • did you consider the issue might be on the php side ? – njzk2 Nov 26 '13 at 21:20
  • I don't see how the difference in the Bitmap creation can effect the php side? Of course everything can happen, but I simply can't see the problem :( – Georgi Nov 26 '13 at 22:19
  • so, using decodeResources it works, using decodeFile it does not, but in both cases you are using the same bitmap.compress and Base64.encode methods ? did you check the size of the sent items, did you check what you receive on the server side ? – njzk2 Nov 26 '13 at 22:21
  • I just did a number of tests and found that the problem is coming from the image size. The question was edited accordingly – Georgi Nov 27 '13 at 10:04
  • you probably need to use multipart posting to send a larger entity to the server (it will be lighter for your android app as well as you won't need to load the whole image and encode it in base64.) – njzk2 Nov 27 '13 at 14:20
  • See this question for an example : http://stackoverflow.com/questions/2017414/post-multipart-request-with-android-sdk – njzk2 Nov 27 '13 at 14:22
  • Thanks! It all works good with the MultipartEntityBuilder,because the MultipartEntity is depricated. If you want you can post an answer and I will mark it – Georgi Dec 04 '13 at 11:27

0 Answers0