如何拍一张照片并发送HTTP POST请求与Android? [英] How to take a photo and send to HTTP POST request with Android?

查看:187
本文介绍了如何拍一张照片并发送HTTP POST请求与Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这有答案,在这里和那里,但我不能让任何人工作。是否有人知道一个很好的参考,或教程对于这一点,也许还张贴在这里?

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)用户需要的相片,并返回到该应用程序,与照片保存,preferably用preVIEW在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)presses一个发送按钮,应用程序发送的图片以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.

有任何的帮助将是AP preciated。谢谢!

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 = "\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();
}
}

用法很简单:把这种静态方法,并通过图片的路径,如:

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.. :)

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆