2012-06-10 3 views
2

카메라에서 가져온 이미지를 두 개 이상 업로드하려고합니다.하나 이상의 이미지 저장 및 업로드

public void TakePicture(int actionCode) 
    { 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     try 
     { 
      photo[0] = createTemporaryFile("spot", ".jpg"); 
     } 
     catch(Exception e) 
     { 
      Log.v("ERROR SD!!", "Can't create file to take picture!"); 
      Toast.makeText(this, "Please check SD card! Image shot is impossible!", 10000); 
     } 

     fileUri = Uri.fromFile(photo[0]); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
     startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 
    } 

을 그리고 나는 PHP 서버에 업로드 : 나는 Intent를 통해 카메라를 호출 photo[0], photo[1]photo[2] :

public void UploadImg() 
    { 
     HttpURLConnection conn = null; 
     DataOutputStream dos = null; 
     DataInputStream inStream = null; 

     // String exsistingFileName = "/sdcard/prueba.png"; --> Used for local files!! 

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

     int bytesRead, bytesAvailable, bufferSize; 
     byte[] buffer; 
     int maxBufferSize = 1*1024*1024; 
     String urlString = "http://myUrl.com/uploadimg.php"; 

     try 
     { 
      FileInputStream fileInputStream = new FileInputStream(photo[0].toString()); 

      // Open a URL connection to the Servlet 
      URL url = new URL(urlString); 

      // Open a HTTP connection to the URL 
      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.writeBytes(twoHyphens + boundary + lineEnd); 
      dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + photo[0] +"\"" + lineEnd); 
      dos.writeBytes(lineEnd); 

      // Create a buffer of maximum size 
      bytesAvailable = fileInputStream.available(); 
      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      buffer = new byte[bufferSize]; 

      // Read file and write it into form... 
      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); 
      } 

      // Send multipart form data necesssary after file data... 
      dos.writeBytes(lineEnd); 
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

      // Close streams 
      fileInputStream.close(); 
      dos.flush(); 
      dos.close(); 
     } 
     catch (MalformedURLException ex) { Log.e("MediaPlayer", "error: " + ex.getMessage(), ex); } 
     catch (IOException ioe) { Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe); } 

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

      while ((str = inStream.readLine()) != null) 
      { 
       System.out.println("Server Response" + str); 
      } 
      inStream.close(); 
     } 
     catch (IOException ioex) { Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex); } 
    } 

나는 3 개 가지 이미지를 저장합니다. 문제는 예를 들어 두 장의 사진을 찍을 때 하나만 업로드하고 size = 0으로 업로드한다는 것입니다. UploadImg()의 코드에서

나는 단지 photo[0] 보여 주지만, 그것을 촬영 한 모든 이미지를 업로드 할 수 있도록 '진짜'코드에서 나는 처음 tryfor loop를 사용합니다.

내가 뭘 잘못하고 있는지 알기 원하십니까?

미리 감사드립니다.

답변

0

나는 이미 내 문제를 해결했습니다! 다음 작업을 수행했습니다 : photoFile을 저장하는 대신 이미지의 위치를 ​​String으로 저장합니다. 서버에 이미지를 업로드 할 때

if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 
       for (int u = 0; u <= 2; u++) 
       { 
        if (savedImgs[u].equals("")) 
        { 
         // Saving important info to be used later 
         imgs = u + 1; 
         savedImgs[u] = photo.toString(); 
         break; 
        } 
       } ... 

그리고는,이 같은 for loop합니다

public void UploadImg() 
    { 
     HttpURLConnection conn = null; 
     DataOutputStream dos = null; 
     DataInputStream inStream = null; 

     // String exsistingFileName = "/sdcard/prueba.png"; --> Used for local files!! 

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

     int bytesRead, bytesAvailable, bufferSize; 
     byte[] buffer; 
     int maxBufferSize = 1*1024*1024; 
     String urlString = "http://myUrl.com/uploadimg.php"; 

     for (int n = 0; n < imgs; n++) 
     { 
      try 
      { 
       FileInputStream fileInputStream = new FileInputStream(savedImgs[n]); 

       // Open a URL connection to the Servlet 
       URL url = new URL(urlString); 

       // Open a HTTP connection to the URL 
       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.writeBytes(twoHyphens + boundary + lineEnd); 
       dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + savedImgs[n] +"\"" + lineEnd); 
       dos.writeBytes(lineEnd); 

       // Create a buffer of maximum size 
       bytesAvailable = fileInputStream.available(); 
       bufferSize = Math.min(bytesAvailable, maxBufferSize); 
       buffer = new byte[bufferSize]; 

       // Read file and write it into form... 
       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); 
       } 

       // Send multipart form data necesssary after file data... 
       dos.writeBytes(lineEnd); 
       dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

       // Close streams 
       fileInputStream.close(); 
       dos.flush(); 
       dos.close(); 
      } 
      catch (MalformedURLException ex) { Log.e("MediaPlayer", "error: " + ex.getMessage(), ex); } 
      catch (IOException ioe) { Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe); } 

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

       while ((str = inStream.readLine()) != null) 
       { 
        System.out.println("Server Response" + str); 
       } 
       inStream.close(); 
      } 
      catch (IOException ioex) { Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex); } 
     } 
    } 
관련 문제