I am currently looking for a way to let the user pick an image from my apps data. The images are currently in the Assets folder, but may be shoved to the internal storage if that's easier.
I don't want all the files to be stored in a public storage.
I am using the following code to pick from the users gallery and it works well. I hope to somehow use a similar code for the current situation.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
I created a "Gallery" myself by using a GridView.
I used the code from that side to create an ImageAdapter, with a few changes:
public class ImageAdapter extends BaseAdapter {
private ArrayList <String> data = new ArrayList();
// I'm using a yamlReader to fill in the data, but it could instead just be hardcoded.
fillDataWithImageNames();
// create a new ImageView for each item referenced by the Adapter
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(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
// The images are in /app/assets/images/thumbnails/example.jpeg
imageView.setImageDrawable(loadThumb("images/thumbnails/" + data.get(position) + ".jpeg"));
return imageView;
}
// references to our images
private Drawable loadThumb(String path) {
try {
// get input stream
InputStream ims = mContext.getAssets().open(path);
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
return d;
}
catch(IOException ex) {
return null;
}
}