0

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

4

1 回答 1

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

异步任务类

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;
      }
   }
于 2013-04-01T11:52:23.580 回答