Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take a photo and send to HTTP POST request with 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?

What I need to do is :

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

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 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 !

like image 499
jeff Avatar asked Nov 04 '13 06:11

jeff


People also ask

How do I post a photo in a request?

Option 1: Direct File Upload , From this method you can select form-data and set the type to file. Then select an image file by clicking on the button shown in the value column. The content type is automatically detect by postman but if you want you can set it with a relevant MIME type.


1 Answers

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

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

and finally:

MultipartServer.postData(url, nameValuePairs);

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


Update

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.

like image 74
Ankit Popli Avatar answered Oct 26 '22 19:10

Ankit Popli