开发者

get the Image URI of images displayed in gridview

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.


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


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜