Peltier Cooler
Peltier Cooler

Reputation: 169

How can I select a directory (using radio buttons) of pictures to display in Java for Android?

Blatant n00b question: I have several directories of pictures and wish to display randomly pictures from only one, which I select by a set of radio buttons. How do I specify the directory when using :

//"ha" is ha.png, which I would like to be at drawable/1/ha.png
image.setImageResource(R.drawable.ha);

Can I use setImageResource for this? If so how? If not, what should I use and how?

The object of the exercise is a flashcard program with different lessons (hence the dividing up of images) selectable at the first activity.

Upvotes: 2

Views: 192

Answers (2)

Gaurav Navgire
Gaurav Navgire

Reputation: 780

You can use a GridView to show the images from a directory selected from a radio button (as your requirement says). After creating a GridView, associate a adapter to it. Please refer below for a n example adapter :

public class ImageAdapter extends BaseAdapter {

    /** LayoutInflater. */
    private LayoutInflater mInflater;

    /** The i. */
    private ImageView i;

    /**
     * Instantiates a new image adapter.
     * 
     * @param c
     *            the c
     */
    public ImageAdapter(Context c) {
        mInflater = LayoutInflater.from(c);
    }

    public int getCount() {
                    // scaled pictures will have the list of
                    // which you have from the directory
        return scaledPictures.size();
    }

    public Bitmap getItem(int position) {
        return scaledPictures.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
        convertView = mInflater.inflate(R.layout.image, parent, false);
        } else {
            i = (ImageView) convertView;
        }

        Bitmap bitmap = getItem(position);
        i = (ImageView) convertView.findViewById(R.id.galleryimage);
        i.setImageBitmap(bitmap);
        bitmap = null;

        return i;
    }
}

Upvotes: 1

Mathias Conradt
Mathias Conradt

Reputation: 28685

You cannot have subfolders under res/drawable, if you are referring to the drawables folder in your apk.

If you are referring to a random folder on your sdcard, then it's fine to use subfolders, but then you cannot use R.drawable.* for that approach to refer to the image.

In that case you need to load the image using

Bitmap bmp = BitmapFactory.decodeFile("/sdcard/drawable/1/ha.png");

which returns a bitmap, which you can use like

image.setImageBitmap(bmp)

see http://developer.android.com/reference/android/widget/ImageView.html#setImageBitmap(android.graphics.Bitmap)

In order to react on changes made to the radion button, see How to set On click listener on the Radio Button in android

Upvotes: 3

Related Questions