Android Multipart图像上传到Azure

时间:2013-04-01 11:43:15

标签: android azure

有没有办法将图像从Android设备上传到Azure存储桶?

1 个答案:

答案 0 :(得分:0)

试试这个让我知道它是否适合你

 public HttpResponse request(String filePath, String url) {

      HttpClient httpclient = new DefaultHttpClient();
      httpclient.getParams()
            .setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

      HttpPost httppost = new HttpPost(url);
      MultipartEntity mpEntity = new MultipartEntity();




         File file = new File(filePath);
         ContentBody cbFile = new FileBody(file, "image/png");
         mpEntity.addPart(PARAMETER_SIGNATURE, cbFile);

      try {

         httppost.setEntity(mpEntity);
         System.out.println("executing request " + httppost.getRequestLine());
         HttpResponse response;
         response = httpclient.execute(httppost);
         httpclient.getConnectionManager().shutdown();
         return response;
      } catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }

AsyncTask类

public class Task extends AsyncTask<String, Void, HttpResponse> {

      private Exception exception;


      protected void onPostExecute(HttpResponse response) {
         Log.i("Loader ", response.getStatusLine().toString());
         // if (resEntity != null) {
         // System.out.println(EntityUtils.toString(resEntity));
         // }

         int status = response.getStatusLine().getStatusCode();
         if (status == 200) {
          Log.i("Loader ", "upload ok");
         } else {
          Log.i("Loader ", "upload failed");
         }

      }

      @Override
      protected HttpResponse doInBackground(String... params) {
         try {

            return request(params[0], params[1]);
         } catch (Exception e) {
            this.exception = e;
            return null;
         }
      }

      public Exception getException() {
         return exception;
      }
   }