0

I am really new to app development and I have been trying to create an image gallery app. But when I click to open the app, nothing is being displayed.I expect it to display images stored on SD card in Gridview. I have read quite a few questions on SO related to it but they haven't been of much help. I can't find anything wrong in my code.Can anyone tell me what is wrong with my code?

imageGallery.java :

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.GridView;

import java.io.File;
import java.util.ArrayList;

public class imageGallery extends Activity {

    ArrayList<String> images;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_gallery);

        images = new ArrayList<>();// list of file paths
        getFromSdcard();

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

    File[] listFile;

    public void getFromSdcard() {
        File file = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "InsCam + FixBlur");
        if (listFile != null && listFile.length > 0) {
            for (File file1 : listFile) {

                if (file1.isDirectory()) {

                    listFile = file.listFiles();
                } else {
                    if (file.getName().endsWith(".png")
                            || file.getName().endsWith(".jpg")
                            || file.getName().endsWith(".jpeg")
                            || file.getName().endsWith(".gif")
                            || file.getName().endsWith(".bmp")
                            || file.getName().endsWith(".webp"))


                    {
                        String temp = file1.getPath().substring(0, file1.getPath().lastIndexOf('/'));
                        if (!images.contains(temp))
                            images.add(temp);
                    }
                }
            }
        }
    }
}

ImageAdapter.java :

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import java.util.List;

public class ImageAdapter extends BaseAdapter {
    private static final int PADDING = 8;
    private static final int WIDTH = 250;
    private static final int HEIGHT = 250;
    private Context mContext;
    private List<String> mThumbIds;

    public ImageAdapter(Context c, List<String> ids){
        mContext = c;
        this.mThumbIds = ids;
    }

    @Override
    public int getCount() {
        return mThumbIds.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    // Will get called to provide the ID that
    // is passed to OnItemClickListener.onItemClick()
    @Override
    public long getItemId(int position) {
        return position;
    }

    // create a new ImageView for each item referenced by the Adapter
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView = (ImageView) convertView;

        // if convertView's not recycled, initialize some attributes
        if (imageView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(WIDTH, HEIGHT));
            imageView.setPadding(PADDING, PADDING, PADDING, PADDING);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(mThumbIds.get(position), options);

        // Set inSampleSize
        options.inSampleSize = 4;

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap myBitmap = BitmapFactory.decodeFile(mThumbIds.get(position), options);

        imageView.setImageBitmap(myBitmap);

        return imageView;
    }
}
Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66

1 Answers1

0

Your getItem method in adapter returns null that seems to be the issue. You should return string path or bitmap after converting the string path what ever you like as return type is Object. so I would suggest this.

class ImageAdapter extends BaseAdapter{

  @Override
   public Object getItem(int position) {
        //I have shown string example here.. you can update as per your needs..
       return mThumbIds.get(position);
   }

}

hope this helps..

Aalap Patel
  • 2,058
  • 2
  • 17
  • 31