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

我知道这里有答案,但是我不能让他们任何一个工作。 有没有人知道一个很好的参考,或者一个教程,也可以在这里发布?

我需要做的是:

1)提供一个button,打开相机应用程序。 我已经做了这个startResultActivity()

2)用户拍摄照片,然后返回到应用程序,保存照片,最好在ImageView中预览。 我尝试了一些东西,但是我不能在一个模拟器件中testing。

3)按下“发送”button,应用程序将图片发送到HTTP POST。 用“多部分”,无论如何。 PHP开发人员不希望我发送图片作为从位图数组转换的string。

任何帮助,将不胜感激。 谢谢 !

Solutions Collecting From Web of "如何使用Android拍摄照片并发送到HTTP POST请求?"

该链接应该足够用于点击,保存和获取图像path: 捕获图像

这是我写的通过HTTP POST上载图片的课程:

 public class MultipartServer { private static final String TAG = "MultipartServer"; private static String crlf = "\r\n"; 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(); } } 

用法很简单:调用这个静态方法,像下面这样传递图像的path:

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

最后:

 MultipartServer.postData(url, nameValuePairs); 

不要忘记在一个单独的线程中调用这个函数,否则你会得到NetworkOnMainThreadException .. 🙂


更新

我build议不要重新发明轮子,而是使用OkHttp 。 检查食谱页面。 免责声明 :我不是该项目的贡献者,但我喜欢它。 感谢广场团队。