gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:56:16 26 4
gpt4 key购买 nike

我知道这里和那里都有答案,但我无法使它们中的任何一个起作用。有没有人知道一个好的引用或教程,也许也张贴在这里?

我需要做的是:

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

完成的

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

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

如有任何帮助,我们将不胜感激。谢谢!

最佳答案

此链接对于单击、保存和获取图像路径应该绰绰有余: Capture Images

这是我为通过 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();
}
}

用法非常简单:调用这个静态方法并传递图像的路径,如下所示:

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

最后:

MultipartServer.postData(url, nameValuePairs);

并且不要忘记在单独的线程中调用此函数,否则您将得到 NetworkOnMainThreadException.. :)


更新

我建议不要重新发明轮子并使用 OkHttp反而。请检查 Recipes页。 免责声明:我不是该项目的贡献者,但我喜欢它。感谢 Square 团队。

关于php - 如何使用 Android 拍照并发送到 HTTP POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19762581/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com