bmm
bmm

Reputation: 13

How to split the image in the chunks of 4MB and upload to server in android

I want to split the image in the chunks for 4mb each and then upload to server.How can I achieve this for sending an image in multipart.

I have tried some way but I am getting media not supported error.

            FileInputStream fileInputStream = new FileInputStream(selectedFile);
            URL url = new URL("SERVER_URL");
            connection = (HttpURLConnection) url.openConnection();

            connection.setDoInput(true);//Allow Inputs
            connection.setDoOutput(true);//Allow Outputs
            connection.setUseCaches(false);//Don't use a cached Copy
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("ENCTYPE", "multipart/form-data");
            connection.setRequestProperty(
                    "Content-Type", "multipart/form-data;boundary=" + boundary);
            connection.setRequestProperty("uploaded_file",selectedFilePath);

I want the input in following way.

    {
              "blocks": [{
                    "hashdata": "dsfhsdfjhjsdfhj36278dhgjgddfsh7k",
                    "data": <base64 data>
                },{
                    "hashdata": "abcdskjfkskdfh8772384374789327dh",
                    "data": <base64 data>
                }]
     }

Upvotes: 1

Views: 3842

Answers (1)

Paul P Joby
Paul P Joby

Reputation: 773

The best and simple way is to read the files as bytes into to a byte array and the divide it into whatever size you want and then upload to the server. This method can be used for dividing any type of file no matter if its an image or text or even binary file.

Make Sure you don't do this on you UI Thread

      final int MAX_SUB_SIZE = 4194304; // 4*1024*1024 == 4MB
      FileInputStream f = new FileInputStream("pathto/img.png");
      byte[] data = new byte[f.available()];  // Size of original file
      byte[] subData = new byte[MAX_SUB_SIZE];  // 4MB Sized Array

      f.read(data); // Read The Data
      int start = 0;  // From 0
      int end = MAX_SUB_SIZE;    // To MAX_SUB_SIZE bytes
      // Just Saving the size to avoid re-computation
      int max = data.length;
      if(max > 0)
      {

           for( ;end < max; ){
             subData = Arrays.copyOfRange(data, start, end);
             start = end;
             end = end + MAX_SUB_SIZE;
             if(end >= max)
             {
                end = max;
             }

             upload4MB(subData); // This is your method to upload data
           }
           // For the Last Chunk (size less than or equal to 4mb)
           end--; // To avoid a padded zero
           subData = Arrays.copyOfRange(data, start, end);
           upload4MB(subData);
         }

NOTE

    /*This method used in the abouve code is your
      logic for uploading the file to sever */

      upload4MB(byte[] chunk) 

    /*Either save the chunks to upload it together
     or upload the chunk then and there itself*/

Put all these in try-catch to capture any kind of exception that may occur.

"subData" byte array corresponds to your 4mb chunk of data do whatever you wish to do with the chunk.

Also refer this post so as to get an idea how to upload file to server using Android and PHP as backend.

Read this Post on StackOverflow that shows how to upload files

Hope this solves your issue. Please don't forget you approve if it was useful.

Upvotes: 6

Related Questions