Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to Upload image into server in Android?
    primarykey
    data
    text
    <p>I am trying to Upload image Capture by Camera into server. server send response code = 200 but Image is not upload into server.</p> <p>Code is : </p> <pre><code>private boolean uploadData() { int count = this.forumThreadsB.size(); for (int i = 0; i &lt; count; i++) { if (isPhoto) message = "Uploading Shared Items " + (i + 1) + " of " + count; else message = "Uploading Shared Items " + (i + 1) + " of " + count; progressCount = (i * 1000)/count; Hashtable&lt;?, ?&gt; threadD = (Hashtable&lt;?, ?&gt;)this.forumThreadsB.elementAt(i); String onlinePath = "http://xyx.com/; threadid = (String) threadD.get("devicethreadid"); Hashtable&lt;String, String&gt; pairs = new Hashtable&lt;String, String&gt;(); pairs.put("forumid", threadD.get("lmsforumid").toString()); pairs.put("topicid", threadD.get("lmsthreadid").toString()); pairs.put("clientid", LoginHelper.clientid); String fullfilepath = threadD.get("offlinepath").toString(); int index = threadD.get("offlinepath").toString().lastIndexOf("/"); String filename = fullfilepath.substring(index + 1); String filetype = ""; if (filename.toLowerCase().contains(".png")) filetype = "image/png"; else if (filename.toLowerCase().contains(".jpg")) filetype = "image/jpeg"; else if (filename.toLowerCase().contains(".mp4")) filetype = "image/mp4"; else if (filename.toLowerCase().contains(".3gp")) filetype = "image/3gpp"; String boundaryMessage = getBoundaryMessage(BOUNDARY, pairs, fullfilepath, filename, filetype); String endBoundary = "\r\n--" + BOUNDARY + "--\r\n"; HttpURLConnection conn = null; DataOutputStream dos = null; String lineEnd = "\r\n"; String twoHyphens = "--"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1*1024*1024; try { URL url = new URL(onlinePath); 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); dos = new DataOutputStream( conn.getOutputStream() ); dos.write( boundaryMessage.getBytes()); File file = new File(fullfilepath.substring(6)); FileInputStream fileInputStream = new FileInputStream(file); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead &gt; 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } dos.write(endBoundary.getBytes()); dos.flush(); dos.close(); fileInputStream.close(); } catch (IOException ioe) { Log.e("SyncUploadDownloadHelper", "Cannot upload file: " + ioe.getMessage(), ioe); //return false; } // Read response try { int responseCode = conn.getResponseCode(); if(responseCode == 200){ SQLiteForumDAO forumDAO = new SQLiteForumDAO(mcontext) ; ForumThreadDTO forumThreadDTO = forumDAO.selectThread(this.threadid); if(downloadPath!=null &amp;&amp; downloadPath.equalsIgnoreCase("null") &amp;&amp; downloadPath.equalsIgnoreCase("")) forumThreadDTO.offlinefilepath = downloadPath; forumDAO.updateThread(forumThreadDTO); } } catch (IOException ioex) { Log.e("SyncUploadDownloadHelper", "Upload file failed: " + ioex.getMessage(), ioex); //return false; } catch (Exception e) { Log.e("SyncUploadDownloadHelper", "Upload file failed: " + e.getMessage(), e); //return false; } if (i == (this.forumThreadsB.size() - 1)){ this.sendStatus = "true"; progressCount = 1000; SyncUploadDownloadHelper.this.notifyObservers("SyncUploadDownloadHelper:UploadDataFinish"); } else SyncUploadDownloadHelper.this.notifyObservers("SyncUploadDownloadHelper:UploadData"); //return true; } return true; } </code></pre> <p>Function : </p> <pre><code>private String getBoundaryMessage(String boundary, Hashtable&lt;String, String&gt; params, String fileField, String fileName, String fileType) { StringBuffer res = new StringBuffer("--").append(boundary).append("\r\n"); Enumeration&lt;String&gt; keys = params.keys(); while(keys.hasMoreElements()) { String key = (String)keys.nextElement(); String value = (String)params.get(key); System.out.println(key + ": " + value); res.append("Content-Disposition: form-data; name=\"").append(key).append("\"\r\n") .append("\r\n").append(value).append("\r\n").append("--").append(boundary).append("\r\n"); } res.append("Content-Disposition: form-data; name=\"").append("file").append("\"; filename=\"").append(fileName).append("\"\r\n") .append("Content-Type: ").append(fileType).append("\r\n\r\n"); return res.toString(); } </code></pre> <p>in my Application I Capture Image and Save it to Database. path of save image is use to upload image file.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload