2011-03-24 16 views
11

मुझे एक ऐसा एप्लिकेशन बनाना है जो फ़ोन से चित्रों को वेब सर्वर पर भेजना चाहिए। दुर्भाग्यवश, मैं वास्तव में नहीं जानता कि यह कैसे करें। क्या कोई मेरी मदद कर सकता है?वेब सर्वर पर चित्र भेजना

+0

का उपयोग करें क्या आप सर्वर स्टैक और संभवतः एपीआई निर्दिष्ट कर सकते हैं? –

+0

उत्तर देने के लिए धन्यवाद, रवि ... यह google apis 1.5 है और मुझे यकीन नहीं है कि सर्वर स्टैक द्वारा आपका क्या मतलब है ... क्या आप कृपया समझा सकते हैं? मैं एंड्रॉइड के लिए थोडा नया हूँ .. –

+1

@Ravi: उन्होंने किसी भी अपवाद के बारे में नहीं कहा था। – Mudassir

उत्तर

0

ऐसा करने के लिए एक HTTP अनुरोध आप DefaultHttpClient वर्ग और HttpPost वर्ग

HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost("http://your.site/your/service"); 
// set some headers if needed 
post.addHeader(....); 
// and an eclosed entity to send 
post.setEntity(....); 
// send a request and get response (if needed) 
InputStream responseStream = client.execute(post)..getEntity().getContent(); 

अनुरोध करने के लिए एक इकाई जोड़ने की विधि कैसे अपने रिमोट serivice काम कर रहा है पर निर्भर करता है का उपयोग कर सकते हैं।

+0

धन्यवाद ओलेगस, नमूना कोड की सराहना करते हैं। मैं कोशिश करूँगा और देखता हूं कि यह कैसा चल रहा है। –

16

इस कार्य को प्राप्त करने के लिए वेब सेवा का उपयोग करें।

एंड्रॉइड में वेब सेवा का उपयोग करने के लिए, कृपया इस लिंक पर जाएं।

  1. kSoap2 एंड्रॉयड डिवाइस से वेब सेवा फोन करने के लिए इस्तेमाल किया पुस्तकालय।
  2. Calling simple web service in android.
  3. Calling web service & uploading file through HttpClient
  4. Web Service That Returns An Array of Objects With KSOAP - जटिल वस्तुओं के लिए।
  5. Accessing a JAX-WS web service from Android
  6. How-to: Android as a RESTful Client
+1

अच्छा .., +1 ..... – Mudassir

+0

चरण-दर-चरण सलाह, शशांक के लिए धन्यवाद। मैं इसे आज़माउंगा और देख सकता हूं कि यह कैसा चल रहा है। –

10

यहाँ कोड मैं एक दूरस्थ सर्वर से छवियों को अपलोड करने, कच्चे सॉकेट का उपयोग कर का उपयोग कर रहा है। Httpclient पर कच्चे सॉकेट के पेशेवर यह है कि आप एक अपलोड प्रगति पट्टी प्रदर्शित कर सकते हैं।

अस्वीकरण: अधिकांश कोड ज्यादातर स्टैक ओवरफ्लो से आता है।

/** 
* Asynchronous task to upload file to server 
*/ 
class UploadImageTask extends AsyncTask<File, Integer, Boolean> { 

    /** Upload file to this url */ 
    private static final String UPLOAD_URL = "http://thibault-laptop:8080/report"; 

    /** Send the file with this form name */ 
    private static final String FIELD_FILE = "file"; 
    private static final String FIELD_LATITUDE = "latitude"; 
    private static final String FIELD_LONGITUDE = "longitude"; 

    /** 
    * Prepare activity before upload 
    */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     setProgressBarIndeterminateVisibility(true); 
     mConfirm.setEnabled(false); 
     mCancel.setEnabled(false); 
     showDialog(UPLOAD_PROGRESS_DIALOG); 
    } 

    /** 
    * Clean app state after upload is completed 
    */ 
    @Override 
    protected void onPostExecute(Boolean result) { 
     super.onPostExecute(result); 
     setProgressBarIndeterminateVisibility(false); 
     mConfirm.setEnabled(true); 
     mDialog.dismiss(); 

     if (result) { 
      showDialog(UPLOAD_SUCCESS_DIALOG); 
     } else { 
      showDialog(UPLOAD_ERROR_DIALOG); 
     } 
    } 

    @Override 
    protected Boolean doInBackground(File... image) { 
     return doFileUpload(image[0], UPLOAD_URL); 
    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     super.onProgressUpdate(values); 

     if (values[0] == 0) { 
      mDialog.setTitle(getString(R.string.progress_dialog_title_uploading)); 
     } 

     mDialog.setProgress(values[0]); 
    } 

    /** 
    * Upload given file to given url, using raw socket 
    * @see http://stackoverflow.com/questions/4966910/androidhow-to-upload-mp3-file-to-http-server 
    * 
    * @param file The file to upload 
    * @param uploadUrl The uri the file is to be uploaded 
    * 
    * @return boolean true is the upload succeeded 
    */ 
    private boolean doFileUpload(File file, String uploadUrl) { 
     HttpURLConnection conn = null; 
     DataOutputStream dos = null; 
     String lineEnd = "\r\n"; 
     String twoHyphens = "--"; 
     String boundary = "*****"; 
     String separator = twoHyphens + boundary + lineEnd; 
     int bytesRead, bytesAvailable, bufferSize; 
     byte[] buffer; 
     int maxBufferSize = 1 * 1024 * 1024; 
     int sentBytes = 0; 
     long fileSize = file.length(); 

     // The definitive url is of the kind: 
     // http://host/report/latitude,longitude 
     uploadUrl += "/" + mLocation.getLatitude() + "," + mLocation.getLongitude(); 

     // Send request 
     try { 
      // Configure connection 
      URL url = new URL(uploadUrl); 
      conn = (HttpURLConnection) url.openConnection(); 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 
      conn.setUseCaches(false); 
      conn.setRequestMethod("PUT"); 
      conn.setRequestProperty("Connection", "Keep-Alive"); 
      conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); 
      publishProgress(0); 

      dos = new DataOutputStream(conn.getOutputStream()); 

      // Send location params 
      writeFormField(dos, separator, FIELD_LATITUDE, "" + mLocation.getLatitude()); 
      writeFormField(dos, separator, FIELD_LONGITUDE, "" + mLocation.getLongitude()); 

      // Send multipart headers 
      dos.writeBytes(twoHyphens + boundary + lineEnd); 
      dos.writeBytes("Content-Disposition: form-data; name=\"" + FIELD_FILE + "\";filename=\"" 
        + file.getName() + "\"" + lineEnd); 
      dos.writeBytes(lineEnd); 

      // Read file and create buffer 
      FileInputStream fileInputStream = new FileInputStream(file); 
      bytesAvailable = fileInputStream.available(); 
      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      buffer = new byte[bufferSize]; 

      // Send file data 
      bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
      while (bytesRead > 0) { 
       // Write buffer to socket 
       dos.write(buffer, 0, bufferSize); 

       // Update progress dialog 
       sentBytes += bufferSize; 
       publishProgress((int)(sentBytes * 100/fileSize)); 

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

      // send multipart form data necesssary after file data 
      dos.writeBytes(lineEnd); 
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
      dos.flush(); 
      dos.close(); 
      fileInputStream.close(); 
     } catch (IOException ioe) { 
      Log.e(TAG, "Cannot upload file: " + ioe.getMessage(), ioe); 
      return false; 
     } 

     // Read response 
     try { 
      int responseCode = conn.getResponseCode(); 
      return responseCode == 200; 
     } catch (IOException ioex) { 
      Log.e(TAG, "Upload file failed: " + ioex.getMessage(), ioex); 
      return false; 
     } catch (Exception e) { 
      Log.e(TAG, "Upload file failed: " + e.getMessage(), e); 
      return false; 
     } 
    } 

    private void writeFormField(DataOutputStream dos, String separator, String fieldName, String fieldValue) throws IOException 
    { 
     dos.writeBytes(separator); 
     dos.writeBytes("Content-Disposition: form-data; name=\"" + fieldName + "\"\r\n"); 
     dos.writeBytes("\r\n"); 
     dos.writeBytes(fieldValue); 
     dos.writeBytes("\r\n"); 
    } 
} 

अपलोड प्रारंभ करने के लिए, निम्न आदेश का उपयोग:

new UploadImageTask().execute(new File(imagePath)); 
1

मैं एक रेस्ट वेब सेवा और एंड्रॉयड में DefaultHttpClient वर्ग का उपयोग कर किया। एक नमूना बाकी वेब सेवा बनाने के लिए और अपाचे बिलाव में तैनात करने के लिए, कृपया का पालन करें ट्यूटोरियल Vogella

बाकी सेवा छवि को स्वीकार करने के लिए, बहुखण्डीय सामग्री प्रकार सर्वर साइड

@POST 
@Consumes(MediaType.MULTIPART_FORM_DATA) 
@Produces("application/json") 
public String uploadFile(@FormDataParam("image") InputStream uploadedInputStream, 
     @FormDataParam("image") FormDataContentDisposition fileDetail) { 

    String uploadedFileLocation = "e://game/" + fileDetail.getFileName(); 
    boolean response=false; 
    // save it 
    try{ 
     OutputStream out = null; 
     int read = 0; 
     byte[] bytes = new byte[1024]; 
     out = new FileOutputStream(new File(uploadedFileLocation)); 
     while ((read = uploadedInputStream.read(bytes)) != -1) { 
      out.write(bytes, 0, read); 
     } 
     out.flush(); 
     out.close(); 
     return response=true; 
    }catch(IOException e){ 
     e.printStackTrace(); 
    } 
    return response; 

} 

में आवश्यक है एंड्रॉयड पक्ष में छवि भेजने के लिए (मैंने AsyncTask के doInBackground के भीतर किया था)

  HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost postRequest = new HttpPost("http://"+ip+":8080/MiniJarvisFaceServer/image"); 
      MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
      reqEntity.addPart("image", new FileBody(file)); 
      postRequest.setEntity(reqEntity); 
      ResponseHandler<String> handler = new BasicResponseHandler();   
      String response = httpClient.execute(postRequest,handler); 
      Log.d("Response", response); 
      httpClient.getConnectionManager().shutdown(); 
0

इस मार्गदर्शिका का पालन करें। यह सर्वर पक्ष में PHP का उपयोग कर रहा है। मैंने एंड्रॉइड स्टूडियो और httpmime.4.3.6 का उपयोग किया और एक आकर्षण की तरह काम किया http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/

यह वीडियो का भी समर्थन कर रहा है और यह दिखा रहा है कि आप सर्वर से कुछ परिणामों के साथ कैसे प्रतिक्रिया दे सकते हैं। एकमात्र मुश्किल बिट यह सुनिश्चित कर रहा है कि आप Android के लिए HttClient और HttpMime के सही संस्करण का उपयोग कर रहे हैं। अभी HttpMime 4.4.x काम नहीं कर रहा है और यह मेरे समय का एक सप्ताह बर्बाद हो गया है। 4.3.6

संबंधित मुद्दे