Select Multiple Images Using GalleryView

Posted by hwrdprkns on Stack Overflow See other posts from Stack Overflow or by hwrdprkns
Published on 2010-06-17T04:44:06Z Indexed on 2010/06/17 4:53 UTC
Read the original article Hit count: 366

Filed under:
|
|

Hi guys,

I was just wondering if Android had built in code so that I could select multiple images in a gallery-view and then have those images exported as filenames in a string array(ex /sdcard/~f1.jpg, /sdcard/~f2.jpg,...).

I have the gallery code here, but I'm not sure what modifications need to be made. Any help is appreciated. Thanks!

// take_picture = (Button)findViewById(R.id.take_picture);

    // Here we set up a string array of the thumbnail ID column we want to
    // get back
    String[] proj = { MediaStore.Images.Thumbnails._ID };

    if(proj.length == 0)
    {
        nopic.setVisibility(View.VISIBLE);
    }
    // Now we create the cursor pointing to the external thumbnail store
    cursor = managedQuery(
            MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, proj, // Which
            // columns
            // to
            // return
            null, // WHERE clause; which rows to return (all rows)
            null, // WHERE clause selection arguments (none)
            null); // Order-by clause (ascending by name)
    /*
    take_picture.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent i = new Intent(GalleryActivity.this,
                    CameraActivity.class);
            startActivity(i);

        }
    });
    */

    // We now get the column index of the thumbnail id
    column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

    // Reference the Gallery view
    g = (Gallery) findViewById(R.id.gallery);

    if(proj.length == 0)
    {
        nopic.setVisibility(View.VISIBLE);
        g.setVisibility(View.GONE);
    }

    // Set the adapter to our custom adapter (below)
    g.setAdapter(new ImageAdapter(this));

    // Set a item click listener, and just Toast the clicked position
    g.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position,
                long id) {
            // Now we want to actually get the data location of the file
            String[] proj = { MediaStore.Images.Media.DATA };
            // We request our cursor again
            cursor = managedQuery(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, // Which
                    // columns
                    // to
                    // return
                    null, // WHERE clause; which rows to return (all rows)
                    null, // WHERE clause selection arguments (none)
                    null); // Order-by clause (ascending by name)
            // We want to get the column index for the data uri
            column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            // Lets move to the selected item in the cursor
            cursor.moveToPosition((int) g.getSelectedItemId());
            // And here we get the filename
            String filename = cursor.getString(column_index);
            Log.v("GalleryActivity", filename);
            Toast.makeText(GalleryActivity.this, filename,
                    Toast.LENGTH_SHORT).show();
            setPrefs(filename);
            Intent i = new Intent(GalleryActivity.this, OtherClass.class);
            startActivity(i);
        }
    });
}

And the ImageAdapter code here:

public class ImageAdapter extends BaseAdapter {

    int mGalleryItemBackground;

    public ImageAdapter(Context c) {
        mContext = c;
        // See res/values/attrs.xml for the that defines
        // Gallery1.
        TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
        mGalleryItemBackground = a.getResourceId(
                R.styleable.Gallery_android_galleryItemBackground, 0);
        a.recycle();
    }

    public int getCount() {
        return cursor.getCount();
    }

    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);
        if (convertView == null) {
            cursor.moveToPosition(position);
            int id = cursor.getInt(column_index);
            i.setImageURI(Uri.withAppendedPath(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
                            + id));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setLayoutParams(new Gallery.LayoutParams(200, 200));
            // The preferred Gallery item background
            i.setBackgroundResource(mGalleryItemBackground);
        }
        return i;
    }
}

Again any help is appreciateds! Just to let you guys know, the gallery works fine (for one image) as in it exports the filename correctly. Just need to know if there is an easy way to select multiples and export them. Thanks again!

© Stack Overflow or respective owner

Related posts about android

Related posts about gui