Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android upload image from gallery to server [closed]

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

like image 693
sbnarra Avatar asked Mar 19 '12 12:03

sbnarra


People also ask

How do I upload an image to a server?

Check the image size: Some hosting services don't allow files over a certain size. Upload using an FTP program or image hosting service. Use your web server's hyperlink function to link your URL. Alternatively, link to the image using the page's HTML code.


1 Answers

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");
    }
like image 78
Parth Dani Avatar answered Sep 22 '22 01:09

Parth Dani