【问题标题】:Save and upload more than one image保存并上传多张图片
【发布时间】:2012-06-11 17:27:55
【问题描述】:

我正在尝试上传多个从相机拍摄的图像。我通过Intent调用摄像头:

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 服务器:

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 个不同的图像:photo[0]photo[1]photo[2]。问题是,例如,当我拍摄两张照片时,它只上传其中一张并带有size = 0

UploadImg() 的代码中,我只显示photo[0],但在“真实”代码中,我在第一个try 之后使用for loop,以便上传所有拍摄的图像。

知道我做错了什么吗?

非常感谢您!

【问题讨论】:

    标签: android file-upload bitmap


    【解决方案1】:

    我的问题已经解决了!我做了以下事情:我没有保存photoFile,而是将它保存在带有图像位置的字符串中。

    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); } 
             }
        }
    

    【讨论】:

      猜你喜欢
      • 2014-04-02
      • 1970-01-01
      • 2015-04-20
      • 2022-10-17
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      相关资源
      最近更新 更多