0

I'm trying send a large video to PHP server from App Android, but I can't because always It's reports java.lang.OutOfMemoryError.

I try to use, Apache components and HttpURLConnection but I can't send it...

I read a lot of similar questions but I can't do that.

my code

private class MyAsyncTask extends AsyncTask<String, Integer, Double>
    {
        @Override
        protected Double doInBackground (String... params)
        {
            String datos = value.getText().toString();
            // Create a new HttpClient and Post Header
            HttpClient httpClient = getNewHttpClient();

            HttpPost httppost = new HttpPost("My url");

try
            {

String youFilePathVideo = Environment.getExternalStorageDirectory()
                        + "/project/video.mp4";

                File file = new File(youFilePathVideo);
                byte[] fileData = new byte[(int) file.length()];
                DataInputStream dis = new DataInputStream(new FileInputStream(file));
                dis.readFully(fileData);
                dis.close();

                strBaseVideo=Base64.encodeToString(fileData, 0);

                JSONObject video = new JSONObject();
                try
                {
                    video.put("video.mp4", strBaseVideo);



                } catch (JSONException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                JSONArray jsonArray = new JSONArray();

                jsonArray.put(video);

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("video", video.toString()));

 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpClient.execute(httppost);

                responseBody = EntityUtils.toString(response.getEntity());

Please, I need help, thanks.

3
  • 1
    do not encode, Please send the file only use apache library for multipart upload Commented Mar 17, 2015 at 12:47
  • 1
    obviously file is too big to load it to memory, you have to use some streaming API ... so: first, do not use base64 for sending the file ... send it as plain binary - of course you have to change the server side, too Commented Mar 17, 2015 at 12:48
  • I going to try @sunil Commented Mar 17, 2015 at 12:49

1 Answer 1

4

Your problem is not the JSON string. It is more likely the video file being very large and you are loading it into the memory:

DataInputStream dis = new DataInputStream(new FileInputStream(file));
dis.readFully(fileData);
dis.close();

Why do you need these 3 lines?

Sign up to request clarification or add additional context in comments.

1 Comment

mmm maybe you have the reason, then delete that 3 lines?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.