0

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" />
Disney
  • 1
  • `At the moment I have 2 images`. Those are images from resource. Which you packed at compile time in your apk. They have nothing to do with image files from your device. – greenapps Apr 18 '18 at 14:25
  • I know. They are just like place holders sorta. I know how to use images from res. What I would like to know is how to use images from a folder on my device. Can you help, please? – Disney Apr 18 '18 at 14:59
  • You did not explain the actual problems you have. – greenapps Apr 18 '18 at 15:02
  • Sorry if I was unclear. My problem is that I don't know how to display files from a folder on my devices external storage in the gridview? – Disney Apr 18 '18 at 15:04
  • You are only repeating yourself. We already knew that. Tell your actual problems instead. – greenapps Apr 18 '18 at 15:15
  • [https://stackoverflow.com/questions/31154777/how-to-read-image-from-internal-memory-in-android](https://stackoverflow.com/questions/31154777/how-to-read-image-from-internal-memory-in-android) – Bö macht Blau Apr 18 '18 at 17:45
  • Thanx 0x0nosugar, but the folder whose contents I want to put in the gridview is on the external storage – Disney Apr 18 '18 at 19:37

0 Answers0