Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HI
can anyone tell me how to upload a pic in android....from a USB to a Database and using a button upload to get that image
Posted
Updated 22-May-12 20:37pm
v2
Comments
lewax00 22-May-12 11:49am    
Upload to where? From what source? You need to add some more details to your question (use the "Improve question" link at the bottom of your question).
zamani2 23-May-12 2:38am    
Owk i just did....from a USB to a Database and using a button upload to get that image

1 solution

You can try the below code:

C#
private boolean handlePicture(String filePath, String mimeType) {       
    HttpURLConnection connection = null;
    DataOutputStream outStream = null;
    DataInputStream inStream = null;

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";

    int bytesRead, bytesAvailable, bufferSize;

    byte[] buffer;

    int maxBufferSize = 1*1024*1024;

    String urlString = "http://www.yourwebserver.com/youruploadscript.php";

    try {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(new File(filePath));
        } catch(FileNotFoundException e) { }
        URL url = new URL(urlString);
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);            

        outStream = new DataOutputStream(connection.getOutputStream());

        outStream.writeBytes(addParam("someparam", "content of some param", twoHyphens, boundary, lineEnd));                

        outStream.writeBytes(twoHyphens + boundary + lineEnd);
        outStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + filePath +"\"" + lineEnd + "Content-Type: " + mimeType + lineEnd + "Content-Transfer-Encoding: binary" + lineEnd);           
        outStream.writeBytes(lineEnd);

        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

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

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

        fileInputStream.close();
        outStream.flush();
        outStream.close();  
    } catch (MalformedURLException e) {
        Log.e("DEBUG", "[MalformedURLException while sending a picture]");
    } catch (IOException e) {
        Log.e("DEBUG", "[IOException while sending a picture]"); 
    }

    try {
           inStream = new DataInputStream( connection.getInputStream() );
           String str;

           while (( str = inStream.readLine()) != null) {
               if(str=="1") {
                   return true;
               } else {
                   return false;
               }
           }
           inStream.close();
      } catch (IOException e){
          Log.e("DEBUG", "[IOException while sending a picture and receiving the response]");
      }
    return false;
}

private String addParam(String key, String value, String twoHyphens, String boundary, String lineEnd) {
        return twoHyphens + boundary + lineEnd + "Content-Disposition: form-data; name=\"" + key + "\"" + lineEnd + lineEnd + value + lineEnd;
}
 
Share this answer
 
Comments
zamani2 23-May-12 2:38am    
I dont understand the solution you giving me Genesan :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900