11

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

sbnarra
  • 556
  • 4
  • 9
  • 25

2 Answers2

13

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:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        b1 = (Button)findViewById(R.id.Button01);

        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                openGallery(SELECT_FILE1);
            }
        });
    }

    public void openGallery(int req_code) {

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(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);
        cursor.moveToFirst();

        return cursor.getString(column_index);
    }

for downloading images you can do the following code.

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

        } catch(Exception e) {     
          }  
    } 

    private Drawable grabImageFromUrl(String url) throws Exception {
          return Drawable.createFromStream((InputStream)new URL(url).getContent(), "src");
    }
sfmirtalebi
  • 370
  • 7
  • 16
Parth Dani
  • 535
  • 4
  • 19
  • 2
    please note that data.getData(); will NOT work on Samsung devices. Use a pre-inserted Uri instead – Droidman Dec 13 '12 at 21:52
  • Well it works on latest devices atleast ! – Adnan Mulla Apr 29 '13 at 10:20
  • 1
    @AdnanMulla Thanks...for confirming but need to tell u that when i had posted this comment. I had tested on all the device and then only I had placed my comment over here...So if it really helped you please give a up vote so it can also help other – Parth Dani Apr 29 '13 at 13:52
0

I was facing the same problem.

Bitmap image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);

Worked for me

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Zaartha
  • 1,106
  • 9
  • 25