-1

I have been trying to create an imageGallery app to display images in gridview. I want it to display only image directories. But when I open it, images are not displayed. Nothing seems wrong with code. I dont know why its not working.I am not getting any errors or any runtime exceptions. Can anyone help and tell me what am I doing wrong ? Any kind of help is appreciated. Thanks .

imageGallery.java:

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

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

public class imageGallery extends Activity {

    private ArrayList<String> fileList = new ArrayList<>();

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

        GridView imagegrid = (GridView) findViewById(R.id.gridview);
        imagegrid.setAdapter(new MyGridAdapter());
        File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
        getFile(root);
    }

    public ArrayList<String> getFile(File dir) {

        File listFile[] = dir.listFiles();
        if (listFile != null && listFile.length > 0) {
            for (File file : listFile) {
                if (file.isDirectory()) {
                    getFile(file);
                } 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 = file.getPath().substring(0, file.getPath().lastIndexOf('/'));
                        if (!fileList.contains(temp))
                            fileList.add(temp);
                    }
                }
            }
        }
        return fileList;
    }

    public class MyGridAdapter extends BaseAdapter {

        LayoutInflater inflater;
        List<GridViewItem> items;

        public MyGridAdapter() {

            inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }


        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }


        @Override
        public long getItemId(int position) {
            return position;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.grid_item, null);
            }

            TextView text = (TextView) convertView.findViewById(R.id.textView);
            text.setText(items.get(position).getPath());

            ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
            Bitmap image = items.get(position).getImage();

            if (image != null){
                imageView.setImageBitmap(image);
            }
            else {
                // If no image is provided, display a folder icon.
                imageView.setImageResource(R.drawable.adele1);
            }
            return convertView;
        }

    }
}
young wolf
  • 139
  • 1
  • 2
  • 11
  • Possible duplicate of [Glide images sometimes are not loading](https://stackoverflow.com/questions/39335409/glide-images-sometimes-are-not-loading) – Soutzikevich May 13 '19 at 22:36

2 Answers2

0

The problem is you are setting the adapter and trying to get the images, so You adapter didn't get intimate the images changes or not.

And the data in adapter you never set List<GridViewItem> items.

And also getCount should return the size not "0"

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

Edited:

Do like this,

File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
getFile(root);
GridView imagegrid = (GridView) findViewById(R.id.gridview);
imagegrid.setAdapter(new MyGridAdapter());

Adding Glide in your project, add this in your build.gradle file

compile 'com.github.bumptech.glide:glide:3.7.0'

Change your adapter getView like this,

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.grid_item, null);
        }

        TextView text = (TextView) convertView.findViewById(R.id.textView);
        text.setText(fileList.get(position));

        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
        String imagePath = fileList.get(position);

        Glide.with(imageGallery.this).load(imagePath).placeholder( R.drawable.adele1).into(imageView);

        //if (image != null){
            //imageView.setImageBitmap(image);
        //}
        //else {
            // If no image is provided, display a folder icon.
            //imageView.setImageResource(R.drawable.adele1);
        //}
        return convertView;
    }
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
0

I have already answered this here https://stackoverflow.com/a/56111467/3904109... but to help those having a similar problem...

In my case in Glide 4.8.0 the adding the following to ImageView widget into which Glide was loading did the trick, wierd but it works....

android:background="?attr/selectableItemBackgroundBorderless"

Or Also

android:background="@drawable/R.drawable.yourResourceFile

Or Also

android:background="@android:color/transparent"
DragonFire
  • 3,722
  • 2
  • 38
  • 51