0

I am new to android development. I am trying to send android-gallery images to the server. I take a photo from the camera and display the taken images in a gallery view android. Now i need to send those images to the server -database. I have no idea ow to do this. hear is my code up to now.

   <Gallery
    android:id="@+id/gallery"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="#55000000"
    android:gravity="center_vertical"
    android:spacing="16dp" />

MainActivity

public class MainActivity extends Activity implements
    AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {

Button b;
public static int count = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_main);

    b = (Button) findViewById(R.id.btnSelectPhoto);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            selectImage();
        }
    });

    Gallery g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this, ReadSDCard()));
    g.setOnItemSelectedListener(this);
}

private void selectImage() {
    // TODO Auto-generated method stub

    final CharSequence[] options = { "Take Photo", "Choose from Gallery",
            "Cancel" };

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

    builder.setTitle("Add Photo!");

    builder.setItems(options, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int item) {

            if (options[item].equals("Take Photo"))

            {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                final String dir = (android.os.Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolderrr/");
                File f = new File(dir);
                f.mkdir();
                count++;

                Uri uriSavedImage = Uri.fromFile(new File(dir + count
                        + ".jpg"));
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
                startActivityForResult(intent, 1);

            }

            else if (options[item].equals("Choose from Gallery"))

            {

                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(intent, 2);

            }

            else if (options[item].equals("Cancel")) {

                dialog.dismiss();

            }

        }

    });

    builder.show();

}

@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
        long id) {
    /* mSwitcher.setImageResource(mImageIds[position]); */
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}

@Override
public View makeView() {
    ImageView i = new ImageView(this);
    i.setBackgroundColor(0xFF000000);
    i.setScaleType(ImageView.ScaleType.FIT_CENTER);
    i.setLayoutParams(new ImageSwitcher.LayoutParams(
            android.view.ViewGroup.LayoutParams.MATCH_PARENT,
            android.view.ViewGroup.LayoutParams.MATCH_PARENT));
    return i;
}

private List<String> ReadSDCard() {
    List<String> tFileList = new ArrayList<String>();

    // It have to be matched with the directory in SDCard
    File f = new File("/storage/sdcard0/Pictures/picFolderrr/");

    File[] files = f.listFiles();

    for (int i = 0; i < files.length; i++) {
        File file = files[i];
        /* It's assumed that all file in the path are in supported type */
        tFileList.add(file.getPath());
    }
    return tFileList;
}

/* private ImageSwitcher mSwitcher; */

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;
    private List<String> FileList;

    public ImageAdapter(Context c, List<String> fList) {
        mContext = c;
        FileList = fList;

    }

    public int getCount() {
        return FileList.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);

        Bitmap bm = BitmapFactory.decodeFile(FileList.get(position)
                .toString());
        Bitmap re = Bitmap.createScaledBitmap(bm, 60, 60, false);
        i.setImageBitmap(re);

        i.setAdjustViewBounds(true);
        i.setLayoutParams(new Gallery.LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT));

        i.setBackgroundResource(R.drawable.picture_frame);

        return i;
    }
}

}

Akunar
  • 145
  • 1
  • 1
  • 9
  • You need to use webservice to communicate with server. – Aniruddha May 30 '14 at 06:44
  • @Aniruddha yeh I know but what i want to know is whether i can send multiple images from the Galleryview ... how can i send more than4 images at once? – Akunar May 30 '14 at 08:17
  • Yes you can POST multiple images with Android. Are you sure your server is able to receive more images at the same time? – greenapps May 30 '14 at 08:30
  • @greenapps How can I check whethr my server is able to receive multipls images at the same time. actually i want to take photos of particaulr dog and send alll the photos taken to the server. isnt there an tutorial that i can get idea ? should be from a Gallerview – Akunar May 30 '14 at 08:50
  • If you don't even know if your server can handle multiple images then why do you want to send multiple? If you are sure that your server can receive at least one picture at a time then just do a separate POST for every image. And you will know if you ask the one who belongs that server. For the rest it does not matter where your pictures come from. Because the moment you try to upload you need the full path to that image. Does not matter if you determined that path from a camara, a gallery or a file picker. – greenapps May 30 '14 at 08:57

1 Answers1

0

This code uploads data (images, mp3′s, text files etc.) to HTTP server

Uploading files to HTTP server using POST on Android.

suraj shukla
  • 258
  • 1
  • 3
  • 14