Question

I got image from gallery intent. That is i got uri for an image in android. How to write an image or file to httpsurlconnection to upload to server.please help.

Was it helpful?

Solution

InputStream iStream = context.getContentResolver().openInputStream(uri);
//open httpurlconnection and configure.
OutputStream oos = (OutputStream) httpConnection.getOutputStream();
    try
    {
        int c;
        byte bufr[] = new byte[4096];
        while((c = iStream.read(bufr, 0, 4096)) != -1)
        {
            oos.write(bufr, 0, c);
        }
        oos.flush();
    }
    finally
    {
        if(oos != null)
        {
            oos.close();
        }
        iStream.close();
    }

OTHER TIPS

In your java code:

                            File sourceFile = new File("YOUR IMAGE URL")
                        FileInputStream fileInputStream = new FileInputStream(
                                sourceFile);
                        URL url = new URL("your upload url");
                        // Open a HTTP connection to the URL
                        conn = (HttpURLConnection) url.openConnection();
                        conn.setDoInput(true); // Allow Inputs
                        conn.setDoOutput(true); // Allow Outputs
                        conn.setUseCaches(false); // Don't use a Cached Copy
                        conn.setRequestMethod("POST");
                        conn.setRequestProperty("Connection", "Keep-Alive");
                        conn.setRequestProperty("ENCTYPE",
                                "multipart/form-data");
                        conn.setRequestProperty("Content-Type",
                                "multipart/form-data;boundary=" + boundary);
                        conn.setRequestProperty("uploaded_file", filePath);

                        dos = new DataOutputStream(conn.getOutputStream());
                        dos.writeBytes(twoHyphens + boundary + lineEnd);
                        dos.writeBytes("Content-Disposition: form-data;name=\"uploaded_file\";filename=\""
                                + filePath + "\"" + lineEnd);
                        dos.writeBytes(lineEnd);

                        // create a buffer of maximum size
                        bytesAvailable = fileInputStream.available();
                        bufferSize = Math
                                .min(bytesAvailable, maxBufferSize);
                        buffer = new byte[bufferSize];
                        bytesRead = fileInputStream.read(buffer, 0,
                                bufferSize);

                        while (bytesRead > 0) {
                            dos.write(buffer, 0, bufferSize);
                            bytesAvailable = fileInputStream.available();
                            bufferSize = Math.min(bytesAvailable,
                                    maxBufferSize);
                            bytesRead = fileInputStream.read(buffer, 0,
                                    bufferSize);
                        }
                        // send multipart form data necessary after file
                        // data...
                        dos.writeBytes(lineEnd);
                        dos.writeBytes(twoHyphens + boundary + twoHyphens
                                + lineEnd);
                        System.out.println("connection");
                        // Responses from the server (code and message)
                        serverResponseCode = conn.getResponseCode();

                        if (serverResponseCode == 200) {
                            String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
                                    + " http://www.androidexample.com/media/uploads/";
                            System.out.println("message ----" + msg);
                        }

                        fileInputStream.close();
                        dos.flush();
                        dos.close();

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (ProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

And assuming you use PHP API

<?php

$file_path = "PostImage/";

$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
    echo "success";
} else{
    echo "fail";
}

?>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top