1

i am very new to android and i am stuck here. i have a gridview that is showing images from my sdcard, and a check box with each image for multiple selection. Now after selection, i want to send the images as to a servlet in a multipart http request. i think(correct me if i m wrong) that i need the image URIs to do that but i have no way of getting them after the menu item is selected. Here is the code

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
    case R.id.sendPics:
        // TODO - add code to get and send pics
        GridView grid = (GridView) findViewById(R.id.imageGrid);
        ImageAdapter imageAdapter = (ImageAdapter) (grid.getAdapter());

        for (int i = 0; i < grid.getChildCount(); i++)
        {
            Images im = (Images) imageAdapter.getItem(i);
            View view = grid.getChildAt(i);
            CheckBox cb = (CheckBox) view.findViewById(R.id.checkbox);
            // ImageView iv = (ImageView) view.findViewById(R.id.image);

            if (cb.isChecked())
            {
                Log.d(TAG, ">>>>>>>>>>checked check box found<<<<<<<<<<");

                // TODO get the image path hre to send as a multipart http request
            }
        }

        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

i get the image adapter to get the Loaded Image form this post android How to get image path from GridView through onItemClick listener but i cant find any LoadedImage class in my project so totally stuck. any help will be much appreciated.

edit: Images im = (Images) imageAdapter.getItem(i); in this line i cudnt find LoadedImage class so i thought to try with Images which is wrong.

Community
  • 1
  • 1
Khizar
  • 2,288
  • 5
  • 32
  • 53

2 Answers2

3

Use the following logic to get path of all images present in gallery. Create your own PhotoGrid to display all images. store the path of images in arraylist. show the the image in the gridview by taking from uri from the arraylist.

The following arraylist contains path of all images in sd card. OnItemclick you will get which position is clicked and hence from the arraylist you can get what is the path of the image.

ArrayList<String> listOfAllImages = new ArrayList<String>();
String absolutePathOfImage = null;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

            String[] projection = { MediaColumns.DATA,
                    MediaColumns.DISPLAY_NAME };

            cursor = activity.managedQuery(uri, projection, null, null, null);
            column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index);
listOfAllImages.add(absolutePathOfImage);
}

Thanks Deepak

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • @Deepak thanx for the quick reply, i am going to try and post the outcome :) – Khizar Jun 14 '11 at 09:52
  • @Deepak the images dont show now ,previously i was using the android.provider.MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI uri and the images were not showing but now the images are not showing but i do get the correct uri from the check boxes – Khizar Jun 14 '11 at 10:43
  • Create your owngridview. gridview = (GridView) findViewById(R.id.gridview); imageAdapter = new MyImageAdapter(this, imagesFOrGrid); gridview.setAdapter(imageAdapter); in getView() method of MyImageAdapter use the following code to set image imageView.setImageURI(Uri.parse(listOfAllImages .get(position))); – Sunil Kumar Sahoo Jun 14 '11 at 10:47
  • @Deepak this was how i was showing the thumbnails previously in the getView() method `iv.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + id));` Now i am using this `iv.setImageURI(Uri.parse(imagecursor.getString(image_column_index)));` – Khizar Jun 14 '11 at 11:17
  • instead of that use imageView.setImageURI(Uri.parse(listOfAllImages .get(position))) – Sunil Kumar Sahoo Jun 14 '11 at 11:41
  • thanx alot man , its working now , i am not using the list because i am populating the list in the same method ie saving the uri in list and then populating an imageview with it, later i get the uri of the selected image from the list ... its a bit slow but its working ... thanx a lot , now i'll try and use the tips in the other answer to send a multipart request using these URIs :) – Khizar Jun 14 '11 at 12:24
  • i am glad that i am able to help you – Sunil Kumar Sahoo Jun 14 '11 at 12:44
0

you actually have to get the path to the file on sd card. if you are loading it in an image view you probably have it in an object in the adapter (like calling im.getImagePath() or something to get the path in your case) afterwards you can build an Uri if you need it or you can use a file part for the multipart upload which can be created from the image path. from the uri you can just open a stream and have a streamPart for the multipart upload. whichever works better for you has the same result. although using an Uri can get you the images which normally you can't access by path (like contact photos which are stored on the phone memory not the sdCard).

DArkO
  • 15,880
  • 12
  • 60
  • 88
  • thanx for the reply, i will try both these solutions, n see wat works me and then post back :) – Khizar Jun 14 '11 at 09:54