• <tfoot id='hb18D'></tfoot>

        <i id='hb18D'><tr id='hb18D'><dt id='hb18D'><q id='hb18D'><span id='hb18D'><b id='hb18D'><form id='hb18D'><ins id='hb18D'></ins><ul id='hb18D'></ul><sub id='hb18D'></sub></form><legend id='hb18D'></legend><bdo id='hb18D'><pre id='hb18D'><center id='hb18D'></center></pre></bdo></b><th id='hb18D'></th></span></q></dt></tr></i><div id='hb18D'><tfoot id='hb18D'></tfoot><dl id='hb18D'><fieldset id='hb18D'></fieldset></dl></div>

        <small id='hb18D'></small><noframes id='hb18D'>

        • <bdo id='hb18D'></bdo><ul id='hb18D'></ul>
        <legend id='hb18D'><style id='hb18D'><dir id='hb18D'><q id='hb18D'></q></dir></style></legend>

        如何使用 Android 拍照并发送到 HTTP POST 请求?

        时间:2023-10-31

        <small id='11OzE'></small><noframes id='11OzE'>

        • <bdo id='11OzE'></bdo><ul id='11OzE'></ul>
          <legend id='11OzE'><style id='11OzE'><dir id='11OzE'><q id='11OzE'></q></dir></style></legend>
            1. <i id='11OzE'><tr id='11OzE'><dt id='11OzE'><q id='11OzE'><span id='11OzE'><b id='11OzE'><form id='11OzE'><ins id='11OzE'></ins><ul id='11OzE'></ul><sub id='11OzE'></sub></form><legend id='11OzE'></legend><bdo id='11OzE'><pre id='11OzE'><center id='11OzE'></center></pre></bdo></b><th id='11OzE'></th></span></q></dt></tr></i><div id='11OzE'><tfoot id='11OzE'></tfoot><dl id='11OzE'><fieldset id='11OzE'></fieldset></dl></div>
              <tfoot id='11OzE'></tfoot>
                  <tbody id='11OzE'></tbody>
                • 本文介绍了如何使用 Android 拍照并发送到 HTTP POST 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我知道这里和那里都有答案,但我无法让它们中的任何一个起作用.有没有人知道一个很好的参考,或者这个教程,也许也在这里发布?

                  I know this has answers here and there, but I couldn't make any of them work. Does anybody know a good reference, or a tutorial for this, maybe also post here?

                  我需要做的是:

                  1) 提供一个按钮,用于打开相机应用程序.我已经通过 startResultActivity()

                  1) provide a button, that opens the camera application. I have done this by a startResultActivity()

                  2) 用户拍摄照片,然后返回应用程序,保存照片,最好在 ImageView 中预览.我尝试了一些东西,但无法在模拟设备中进行测试.

                  2) user takes the photo, and returns to the application, with the photo saved, preferably with a preview in an ImageView. I tried something, but I cannot test in an emulated device.

                  3) 按下发送"按钮,应用程序将图片发送到 HTTP POST.有了多部分",不管是什么.php 开发人员不希望我将图片作为从位图数组转换而来的字符串发送.

                  3) presses a "send" button, and the application sends the picture to HTTP POST. With "multipart", whatever that is. The php developer does not want me to send the picture as a string converted from a bitmap array.

                  对此的任何帮助将不胜感激.谢谢!

                  Any help for this will be appreciated. Thanks !

                  推荐答案

                  这个链接对于图片的点击、保存和获取路径应该绰绰有余:捕获图像

                  This link should be more than sufficient for clicking, saving and getting path of an image: Capture Images

                  这是我编写的通过 HTTP POST 上传图片的类:

                  This is the class i wrote for uploading images via HTTP POST:

                  public class MultipartServer {
                  
                  private static final String TAG = "MultipartServer";
                  private static String crlf = "
                  ";
                  private static String twoHyphens = "--";
                  private static String boundary =  "*****";
                  private static String avatarPath = null;
                  
                  public static String postData(URL url, List<NameValuePair> nameValuePairs) throws IOException {
                  
                      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                      connection.setReadTimeout(10000);
                      connection.setConnectTimeout(15000);
                      connection.setRequestMethod("POST");
                      connection.setUseCaches(false);
                      connection.setDoInput(true);
                      connection.setDoOutput(true);
                  
                      connection.setRequestProperty("Connection", "Keep-Alive");
                      connection.setRequestProperty("Cache-Control", "no-cache");
                      connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                  
                      String avatarName = null;
                      StringBuilder query = new StringBuilder();
                      boolean first = true;
                      for (NameValuePair pair : nameValuePairs) {
                          if (first)
                              first = false;
                          else
                              query.append("&");
                          query.append(URLEncoder.encode(pair.getName(), "UTF-8"));
                          query.append("=");
                          query.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
                          if ((avatarName = pair.getName()).equals("avatar")) {
                              avatarPath = pair.getValue();
                          }
                  
                      }
                  
                      FileInputStream inputStream;
                      OutputStream outputStream = connection.getOutputStream();
                      DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
                  
                      dataOutputStream.writeBytes(query.toString());
                  
                      // Write Avatar (if any)
                      if(avatarName != null && avatarPath != null) {
                          dataOutputStream.writeBytes(twoHyphens + boundary + crlf);
                          dataOutputStream.writeBytes("Content-Disposition: form-data; name="" + avatarName + "";filename="" + new File(avatarPath).getName() + "";" + crlf);
                          dataOutputStream.writeBytes(crlf);
                  
                          /*Bitmap avatar = BitmapFactory.decodeFile(avatarPath);
                          avatar.compress(CompressFormat.JPEG, 75, outputStream);
                          outputStream.flush();*/
                  
                          inputStream = new FileInputStream(avatarPath);
                          byte[] data = new byte[1024];
                          int read;
                          while((read = inputStream.read(data)) != -1)
                              dataOutputStream.write(data, 0, read);
                          inputStream.close();
                  
                          dataOutputStream.writeBytes(crlf);
                          dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + crlf);
                      }
                  
                      dataOutputStream.flush();
                      dataOutputStream.close();
                  
                      String responseMessage = connection.getResponseMessage();
                      Log.d(TAG, responseMessage);
                  
                      InputStream in = connection.getInputStream();
                      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
                  
                      StringBuilder response = new StringBuilder();
                      char []b = new char[512];
                      int read;
                      while((read = bufferedReader.read(b))!=-1) {
                          response.append(b, 0, read);
                      }
                  
                      connection.disconnect();
                      Log.d(TAG, response.toString());
                      return response.toString();
                  }
                  }
                  

                  用法很简单:调用这个静态方法并传递图片的路径,如下:

                  Usage is quite simple: call this static method and pass the path of your image like:

                  List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                  nameValuePairs.add(new BasicNameValuePair("avatar", imagePath));
                  

                  最后:

                  MultipartServer.postData(url, nameValuePairs);
                  

                  别忘了在单独的线程中调用这个函数,否则你会得到 NetworkOnMainThreadException.. :)

                  and don't forget to call this function in a separate thread or you'll get NetworkOnMainThreadException.. :)

                  我建议不要重新发明轮子使用 OkHttp 代替.请查看 食谱 页面.免责声明:我不是该项目的贡献者,但我喜欢它.感谢 Square 团队.

                  I'd recommend not to reinvent the wheel & use OkHttp instead. Do checkout the Recipes page. Disclaimer: I'm not a contributor to the project, but I love it. Thanks to Square team.

                  这篇关于如何使用 Android 拍照并发送到 HTTP POST 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Azure 是否提供任何以编程方式登录、重置密码或设备身份验证的功能,如 AWS Cognito? 下一篇:使用 PHPUnit 模拟私有方法

                  相关文章

                  1. <small id='XXP2s'></small><noframes id='XXP2s'>

                        <bdo id='XXP2s'></bdo><ul id='XXP2s'></ul>
                      <i id='XXP2s'><tr id='XXP2s'><dt id='XXP2s'><q id='XXP2s'><span id='XXP2s'><b id='XXP2s'><form id='XXP2s'><ins id='XXP2s'></ins><ul id='XXP2s'></ul><sub id='XXP2s'></sub></form><legend id='XXP2s'></legend><bdo id='XXP2s'><pre id='XXP2s'><center id='XXP2s'></center></pre></bdo></b><th id='XXP2s'></th></span></q></dt></tr></i><div id='XXP2s'><tfoot id='XXP2s'></tfoot><dl id='XXP2s'><fieldset id='XXP2s'></fieldset></dl></div>

                      <tfoot id='XXP2s'></tfoot>
                      <legend id='XXP2s'><style id='XXP2s'><dir id='XXP2s'><q id='XXP2s'></q></dir></style></legend>