Send large (video) file(s) from Android to PHP Server
<p>I'm trying to send a video file from my Android device to a server (that uses PHP to process the request). I've used volley for other requests that I've made to that same server, but when I try to send a video file (as an Base64 String) to the Server, I get an OutOfMemoryError. I was wondering if there was a way to send the file over in a different way (maybe using a stream).</p>

<p>This is my current method to send the video file:</p>

<pre><code>private void syncFullVideos() {
    checkPermissions();
    if (pDialog == null || !pDialog.isShowing()) {
        pDialog = new ProgressDialog(this);
        pDialog.setCanceledOnTouchOutside(false);
        pDialog.setMessage(getString(R.string.pDialogSync));
        pDialog.show();
    }
    syncSucces = new boolean[syncVideos.size()];
    Arrays.fill(syncSucces, Boolean.FALSE);
    for (final Video video: syncVideos) {
        String picturepath = video.getImage_path();
        if (fileExists(picturepath)) {
            File videoFile = new File(picturepath);
            FileInputStream fin = null;
            byte[] byte_arr = null;
            try {
                fin = new FileInputStream(videoFile);
                byte_arr = new byte[(int)videoFile.length()];
                fin.read(byte_arr);
            } catch (IOException e) {
                e.printStackTrace();
            }
            final String image_str = Base64.encodeToString(byte_arr, 0);
            Log.d("SyncFullVideos", video.toString());
            String tag_string_req = "string_req";
            final String TAG = AppController.class
                    .getSimpleName();
            String url = "http://android.diggin.io/diggin/v1/videos";

            StringRequest strReq = new StringRequest(Request.Method.PUT,
                    url, new Response.Listener&lt;String&gt;() {

                @Override
                public void onResponse(String response) {
                    Log.d(TAG, response);
                    try {
                        final JSONObject jsonObject = new JSONObject(response);
                        if (!jsonObject.getBoolean("error4")) {
                            int index = jsonObject.getInt("index");
                            syncSucces[index] = true;
                            Log.e("AddSucces(" + (addCount + 1) + "/" + (syncVideos.size()) + ")", video.toString());
                            addCount++;
                            if (addCount == syncVideos.size()) {
                                refreshPhotos();
                            }
                        } else {
                            Log.e("AddFail(" + (addCount + 1) + "/" + (syncVideos.size()) + ")", video.toString());
                            addCount++;
                            if (addCount == syncVideos.size()) {
                                refreshPhotos();
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    //Send message when something goes wrong
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            pDialog.hide();
                            AlertDialog.Builder dlgAlert = new AlertDialog.Builder(PictureActivity.this);
                            dlgAlert.setMessage(R.string.errFullSyncVids);
                            dlgAlert.setPositiveButton(R.string.errBtnOK, null);
                            dlgAlert.setCancelable(true);
                            dlgAlert.create().show();
                        }
                    });
                }
            }) {

                @Override
                protected Map&lt;String, String&gt; getParams() {
                    Map&lt;String, String&gt; params = new HashMap&lt;&gt;();
                    params.put("id", String.valueOf(video.getId()));
                    params.put("im_path", video.getDBImage_path());
                    params.put("image", image_str);
                    params.put("index", String.valueOf(syncVideos.indexOf(video)));
                    return params;
                }
            };

            // Adding request to request queue
            AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
        }
    }
}
</code></pre>

<p>And this is the PHP I use to process the request and send a response (I'm using the REST API):</p>

<pre><code>$app-&gt;put('/videos', function() use ($app) {
    // check for required params
    verifyRequiredParams(array('id','im_path','image','index'));
    $response = array();
    $id = $app-&gt;request-&gt;put('id');
    $im_path = $app-&gt;request-&gt;put('im_path');
    $image = $app-&gt;request-&gt;put('image');
    $index = $app-&gt;request-&gt;put('index');
    $db = new DbHandler();

    // update photo
    $result = $db-&gt;updateVideo($id, $im_path);

    if ($result) {
        $response["error"] = false;
        $response["message"] = "Video updated successfully";
        // Decode Image
        $binary=base64_decode($image);
        header('Content-Type: video/mp4');
        $im_path2 = explode("_",$im_path);
        $im_path2[0] .= 's';
        $im_path2[2] = $im_path;
        $im_path3 = implode("/",$im_path2);
        $filepath = '../images/'.$im_path3;
        $dirname = dirname($filepath);
        if (!is_dir($dirname))
        {
            if(mkdir($dirname, 0777, true)) {
                $response["error2"] = false;
                $response["message2"] = "Directory Created Succesfully";
            } else {
                $response["error2"] = true;
                $response["error3"] = true;
                $response["error4"] = true;
                $response["message2"] = "Failed to create directory. Please try again";
                $response["filepath"] = $filepath;
            }
        }
        // Images will be saved under images folder
        $file = fopen($filepath, 'wb');
        if ($file == false) {
            $response["error3"] = true;
            $response["error4"] = true;
            $response["message3"] = "Failed to open file. Please try again";
            $response["filepath"] = $filepath;
            echoRespnse(200, $response);
        } else {
            // Create File
            $response["error3"] = false;
            $response["message3"] = "File is open and good to write";
            fwrite($file, $binary);
            fclose($file);
            if (file_exists($filepath)) {
                $response["error4"] = false;
                $response["message4"] = "File exists";
                $response["index"] = $index;
                echoRespnse(201, $response);
            } else {
                $response["error4"] = true;
                $response["message4"] = "File doesn't exist";
                echoRespnse(200, $response);
            }
        }
    } else {
        $response["error"] = true;
        $response["error2"] = true;
        $response["error3"] = true;
        $response["error4"] = true;
        $response["message"] = "Failed to update video. Please try again";
        $response["id"] = $id;
        $response["im_path"] = $im_path;
        echoRespnse(200, $response);
    }            
});
</code></pre>