Skip to content Skip to sidebar Skip to footer

Multiple Images Upload In Android Asynchronously Using Phonegap

I have an issue with multiple images upload to php server. The code given below works fine for android 2.x versions, but the same code doesn't work for android 4.x and above. I hav

Solution 1:

Below is the working code I am using for upload image to the server you can use with AsynTask.

//this method takes Inputs (Image name and Image file path where it stored)//and after upload it returns json responsepublic String pushImage(String filepath,String imageName){


    Log.v(TAG, "path is:"+filepath);
    Log.v(TAG, "image name is:"+imageName);
    HttpURLConnectionconn=null;

    DataInputStreaminStream=null;

    StringlineEnd="\r\n";
    StringtwoHyphens="--";
    Stringboundary="---------------------------14737809831466499882746641449";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    intmaxBufferSize=1*1024*1024;


    String reponse_data=null;
    try{
        //------------------ CLIENT REQUEST      //set request properties// here baseURL is url for uploading imageURLurl=newURL(baseURL);        
        conn = (HttpURLConnection) url.openConnection();        
        conn.setDoInput(true);       
        conn.setDoOutput(true);       
        conn.setUseCaches(false);       
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);



        //add request body        DataOutputStreamdos=newDataOutputStream( conn.getOutputStream() );

        dos.writeBytes(lineEnd+twoHyphens + boundary + lineEnd);        

        dos.writeBytes("Content-Disposition: form-data; name=\"uploaded\";filename=\"" + imageName + "\"" + lineEnd);     

        dos.writeBytes("Content-Type: application/octet-stream\r\n\r\n");

        //now add image dataFileInputStreamfileInputStream=newFileInputStream(newFile(filepath) );

        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = newbyte[bufferSize];
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0){
            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }     

        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);


        fileInputStream.close();
        dos.flush();
        dos.close();
    }
    catch (MalformedURLException ex){
       ex.printStackTrace();
    }
    catch (IOException ioe){
        ioe.printStackTrace();
    }


    //------------------ read the SERVER RESPONSEtry {
        if(conn !=null){
            inStream = newDataInputStream ( conn.getInputStream() );
        }

        String str=null;

        if(inStream !=null){
             while (( str = inStream.readLine()) != null){
                 Log.e("Debug","Server Response "+str);
                 reponse_data=str;
             }


             inStream.close();
        }


    }
    catch (IOException ioex){
        ioex.printStackTrace();
    }
   return reponse_data;
}

you can download commons-io-2.4.jar from this link

Solution 2:

I have got a solution for this and it's here Multiple Image Upload using AsyncTask in Android using Phonegap

Either ways the code seems to work fine on all devices once the following line of code is removed

conn.setChunkedStreamingMode(0);

Post a Comment for "Multiple Images Upload In Android Asynchronously Using Phonegap"