Android 使用asynctask将图像发送到webservice

Android 使用asynctask将图像发送到webservice,android,image,web-services,android-asynctask,uri,Android,Image,Web Services,Android Asynctask,Uri,我有一个应用程序,可以拍摄一张照片,并提供给我一个给定图片的uri。 接下来,我要做的是将这张图片发送到我的Web服务 但是,一旦我尝试了这个方法,我就遇到了一个以前遇到过的错误,这与异步任务有关。因此,为了解决这个问题,我得到了一个HttpManager类,它保存了关于如何连接到web服务、url本身以及它在哪里处理图像uri的信息 public static String uploadImageToWebservice(String uri, String imageUri) { H

我有一个应用程序,可以拍摄一张照片,并提供给我一个给定图片的uri。 接下来,我要做的是将这张图片发送到我的Web服务

但是,一旦我尝试了这个方法,我就遇到了一个以前遇到过的错误,这与异步任务有关。因此,为了解决这个问题,我得到了一个HttpManager类,它保存了关于如何连接到web服务、url本身以及它在哪里处理图像uri的信息

public static String uploadImageToWebservice(String uri, String imageUri) {
    HttpURLConnection connection = null;
    int responseCode = 0;
    String image = "";
    try {

        URL url = new URL(uri + imageUri);
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Content-Type", "text/plain");

        /*
        * String userpassword = adminUser + ":" + adminPassword; String
        * encodedAuthorization = DatatypeConverter
        * .printBase64Binary(userpassword.getBytes("UTF-8"));
        * connection.setRequestProperty("Authorization", "Basic " +
        * encodedAuthorization);
        */

        InputStream is = connection.getInputStream();
        image = is.toString();
        responseCode = connection.getResponseCode();
        connection.disconnect();
    } catch (IOException e) {
        Log.e("URL", "failure response from server >" + e.getMessage()
                + "<");

    } finally {
        if (connection != null) {
            connection.disconnect();
        }

    }
    return image;
}
publicstaticstringuploadImageTowerService(stringURI,stringImageURI){
HttpURLConnection=null;
int-responseCode=0;
字符串图像=”;
试一试{
URL=新URL(uri+imageUri);
connection=(HttpURLConnection)url.openConnection();
connection.setDoOutput(真);
connection.setInstanceFollowDirections(false);
connection.setRequestMethod(“GET”);
setRequestProperty(“内容类型”、“文本/普通”);
/*
*字符串userpassword=adminUser+:“+adminPassword;字符串
*encodedAuthorization=数据类型转换器
*.printBase64Binary(userpassword.getBytes(“UTF-8”);
*connection.setRequestProperty(“授权”、“基本”+
*编码(授权);
*/
InputStream is=connection.getInputStream();
image=is.toString();
responseCode=connection.getResponseCode();
连接断开();
}捕获(IOE异常){
Log.e(“URL”,“来自服务器的故障响应>”+e.getMessage()

+“使用代码上载图像

            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1 * 1024 * 1024;

                   FileInputStream fileInputStream = new FileInputStream("sourcefile");
                    URL url = new URL("URL");

                    // Open a HTTP connection to the URL
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setDoInput(true); // Allow Inputs
                    conn.setDoOutput(true); // Allow Outputs
                    conn.setUseCaches(false); // Don't use a Cached Copy
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Connection", "Keep-Alive");
                    conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                    conn.setRequestProperty("Content-Type",
                            "multipart/form-data;boundary=" + boundary);
                    conn.setRequestProperty("bill", sourceFileUri);

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

                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"bill\";filename=\""
                            + sourceFileUri + "\"" + 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);

                    // Responses from the server (code and message)
                    serverResponseCode = conn.getResponseCode();
                    // String serverResponseMessage = conn
                    // .getResponseMessage();

                    if (serverResponseCode == 200) {
                        statud = "uploaded";
                        // messageText.setText(msg);
                        // Toast.makeText(ctx, "File Upload Complete.",
                        // Toast.LENGTH_SHORT).show();
                        // System.out.println("Uploaded successfyuly http: 200");
                        // recursiveDelete(mDirectory1);

                    }

                    // close the streams //
                    fileInputStream.close();
                    dos.flush();
                    dos.close();
php代码

<?php

   $uploads_dir = './images/';
   $tmp_name = $_FILES['bill']['tmp_name'];
   $pic_name = $_FILES['bill']['name'];

   if (move_uploaded_file($tmp_name, $uploads_dir.$pic_name )) {
   echo "uploaded"; 
   }

?>


这行不通,因为我正在使用android,所以我需要包含asynctask。否则,如果不使用它,我会出现异常。你必须在asynctask中的doinbackground方法中使用此代码。okI很快就会尝试此操作!有太多变量没有在你的代码@raj中说明-你能帮我解决吗?如果你需要上传图像到服务器端,首先使用Base64将其编码为字符串,然后在服务器端将其解码为图像。这将有助于减少android应用程序中的内存异常处理。
<?php

   $uploads_dir = './images/';
   $tmp_name = $_FILES['bill']['tmp_name'];
   $pic_name = $_FILES['bill']['name'];

   if (move_uploaded_file($tmp_name, $uploads_dir.$pic_name )) {
   echo "uploaded"; 
   }

?>