Arun Badole
Arun Badole

Reputation: 11097

How to save/upload image to server on Android?

I want to create an app where user can select a photo from the Photo Album or capture it from Camera. I want to upload the selected or captured photo to server.

How can I do this?

Upvotes: 2

Views: 7517

Answers (2)

rabby
rabby

Reputation: 335

you take the gallery image dialog box by using this code.

mUploadButton = (Button) findViewById(R.id.uploadButton);

mUploadButton.setOnClickListener(new OnClickListener() {//this button is used for pickup gallery image.
        public void onClick(View v) {

            Intent rselect = new Intent(Intent.ACTION_GET_CONTENT, null);
            rselect.setType("image/*");
            rselect.putExtra("return-data", true);
            startActivityForResult(rselect, 1);

        }
    });

onActivityResult() is used for

@Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
     switch (requestCode) {
         case 1:
             if(requestCode == 1 && data != null && data.getData() != null){
                // Bundle params = new Bundle();
                // params.putString("method", "photos.upload");
                // intent's getData() returns a Uri describing 
                // the data which the intent holds
                 Uri _uri = data.getData();

                 if (_uri != null) {
                     //User had pick an image.

                     Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
                     cursor.moveToFirst();

                     //Link to the image
                     final String imageFilePath = cursor.getString(0);
                     Log.v("imageFilePath", imageFilePath);
                     File photos= new File(imageFilePath);
                     long length = photos.length();

                     byte[] imgData = new byte[(int) length];

                     FileInputStream pdata = null;
                        try {
                            pdata = new FileInputStream(photos);


                        } catch (FileNotFoundException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                         try {
                            pdata.read(imgData);//imgdata is an array where you get byte data for selected image from gallery and ready to upload.
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }




                     cursor.close();
                 }
             }
             super.onActivityResult(requestCode, resultCode, data);
         }
     }

Upvotes: 2

rabby
rabby

Reputation: 335

"imgData" is an byte array. this data is now ready to upload on server. you do this by using http post method.

Upvotes: 0

Related Questions