Arran Arran - 1 year ago 122
Android Question

Android upload image from gallery to server

I am trying to upload an image from the Androids photo gallery to a server. All the communication I have done has been with Object Streams but now I am unsure as to how I would do this. Oh and I have used an Input Stream to download an image where you point directly to the image using an URL. If someone could point me in the right direction would be appreciated.

Thank You

Answer Source

For uploading images to a server from your application you can follow following tutorials:

  1. Uploading files to HTTP server using POST on Android.

  2. Upload image or file using http POST multi-part.

The above two url will explain you how to upload images from your application to server.

For uploading image from your photo gallery you require the path of that image file and replace the obtained path with /data/file_to_send.mp3 in first url.

To obtain path of the image from the mobile gallery you can follow the following code:

    public void onCreate(Bundle savedInstanceState) {

        b1 = (Button)findViewById(;

        b1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

    public void openGallery(int req_code) {

        Intent intent = new Intent();
                               "Select file to upload "), req_code);

    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {
            Uri selectedImageUri = data.getData();

            if (requestCode == SELECT_FILE1) {
                selectedPath1 = getPath(selectedImageUri);
                System.out.println("selectedPath1 : " + selectedPath1);

            if (requestCode == SELECT_FILE2) {
                selectedPath2 = getPath(selectedImageUri);
                System.out.println("selectedPath2 : " + selectedPath2);

            tv.setText("Selected File paths : " + selectedPath1 + "," + selectedPath2);

    public String getPath(Uri uri) {

        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        return cursor.getString(column_index);

for downloading images you can do the following code.

    ImageView image = (ImageView)findViewById(;
    if(!ImageUrl.equals("no image")) {          
        try {

        } catch(Exception e) {     

    private Drawable grabImageFromUrl(String url) throws Exception {
          return Drawable.createFromStream((InputStream)new URL(url).getContent(), "src");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download