I'm trying to create a custom gallery that will show all the images in a certain folder on the external storage. I've tried everyhting I could find on stackoverflow and haven't been able to find something that works for me yet.At the moment I have 2 images that you get when you create a new Android Studio project hardcoded into the adapter. The code I have so far uses GridView, following the Android developer tutorial, https://developer.android.com/guide/topics/ui/layout/gridview.html, so if someone could give an answer which uses the android developer gridview code it would be great. I'm new to stackoverflow, and Android development and coding in general, so if you could explain the why of it a little too I would apprciate it. Here's the code as far as I have it working:
GalleryActivity.java (main)
public class GalleryActivity extends AppCompatActivity {
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
gridView = findViewById(R.id.grid_galllery);
gridView.setAdapter(new ImageAdapter(this));
}
}
ImageAdapter.java (adapter)
class ImageAdapter extends BaseAdapter {
Context mContext;
public ImageAdapter (Context context) {
mContext = context;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent
{
ImageView mImageView;
if (convertView == null) {
mImageView = new ImageView(mContext);
mImageView.setLayoutParams(new ViewGroup.LayoutParams(85,
85));
mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
mImageView.setPadding(8, 8, 8, 8);
}
else {
mImageView = (ImageView) convertView;
}
mImageView.setImageResource(mThumbIds[position]);
return mImageView;
}
private Integer[] mThumbIds = {
R.drawable.ic_launcher_background,
R.drawable.ic_launcher_foreground
};
activity_gallery.xml (main layout)
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_galllery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />