How to add an existing image?
<p>I'm editing an open source app: A simple coloring page app for kids. I need to be able to make the user import his own images to be colored,a code for a button in my menu that when clicked by the user it opens his gallery(on SD) to choose an image from and then import this image to the view where he could color it  . <a href="https://f-droid.org/repo/org.androidsoft.coloring_1_src.tar.gz" rel="nofollow">Here is the full source code.</a></p>

<p>And here is the code for loading images from <code>R.drawable</code>:</p>

<pre><code>public class StartNewActivity extends NoTitleActivity implements View.OnClickListener
{
    // This is an expensive operation.

    public static int randomOutlineId()
    {
        return new ResourceLoader().randomOutlineId();
    }

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

        // Apparently this cannot be set from the style.
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

        setContentView(R.layout.start_new);

        GridView gridview = (GridView) findViewById(R.id.start_new_grid);
        gridview.setAdapter(new ImageAdapter(this));
    }

    public void onClick(View view)
    {
        setResult(view.getId());
        finish();
    }

    private static class ResourceLoader
    {

        ResourceLoader()
        {
            // Use reflection to list resource ids of thumbnails and outline
            // images.First, we list all the drawables starting with the proper
            // prefixes into 2 maps.
            Map&lt;String, Integer&gt; outlineMap = new TreeMap&lt;String, Integer&gt;();
            Map&lt;String, Integer&gt; thumbMap = new TreeMap&lt;String, Integer&gt;();
            Field[] drawables = R.drawable.class.getDeclaredFields();
            for (int i = 0; i &lt; drawables.length; i++)
            {
                String name = drawables[i].getName();
                try
                {
                    if (name.startsWith(PREFIX_OUTLINE))
                    {
                        outlineMap.put(name.substring(PREFIX_OUTLINE.length()),
                                drawables[i].getInt(null));
                    }
                    if (name.startsWith(PREFIX_THUMB))
                    {
                        thumbMap.put(name.substring(PREFIX_THUMB.length()),
                                drawables[i].getInt(null));
                    }
                }
                catch (IllegalAccessException e)
                {
                }
            }
            Set&lt;String&gt; keys = outlineMap.keySet();
            keys.retainAll(thumbMap.keySet());
            _outlineIds = new Integer[keys.size()];
            _thumbIds = new Integer[keys.size()];
            int j = 0;
            Iterator&lt;String&gt; i = keys.iterator();
            while (i.hasNext())
            {
                String key = i.next();
                _outlineIds[j] = outlineMap.get(key);
                _thumbIds[j] = thumbMap.get(key);
                j++;
            }
        }

        public Integer[] getThumbIds()
        {
            return _thumbIds;
        }

        public Integer[] getOutlineIds()
        {
            return _outlineIds;
        }

        public int randomOutlineId()
        {
            return _outlineIds[new Random().nextInt(_outlineIds.length)];
        }
        private static final String PREFIX_OUTLINE = "outline";
        private static final String PREFIX_THUMB = "thumb";
        private Integer[] _thumbIds;
        private Integer[] _outlineIds;
    }

    private class ImageAdapter extends BaseAdapter
    {

        ImageAdapter(Context c)
        {
            _context = c;
            _resourceLoader = new ResourceLoader();
        }

        public int getCount()
        {
            return _resourceLoader.getThumbIds().length;
        }

        public Object getItem(int i)
        {
            return null;
        }

        public long getItemId(int i)
        {
            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent)
        {
            ImageView imageView;
            if (convertView == null)
            {
                // If it's not recycled, initialize some attributes
                imageView = new ImageView(_context);
                imageView.setLayoutParams(new GridView.LayoutParams(145, 145));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
                imageView.setOnClickListener(StartNewActivity.this);
            }
            else
            {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(_resourceLoader.getThumbIds()[position]);
            imageView.setId(_resourceLoader.getOutlineIds()[position]);
            return imageView;
        }
        private Context _context;
        private ResourceLoader _resourceLoader;
    }
}
</code></pre>
