Search code examples
androidandroid-fragmentsandroid-libraryandroid-gallery

Creating a Gallery with GridView in Fragment?


I am trying to create a Gallery with gridview in Fragment named GalleryFrag using below code

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
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 java.io.File;
import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 */
public class GalleryFrag extends Fragment {
    ArrayList<String> f = new ArrayList<>();
    File[] files;

    public GalleryFrag() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_gallery, container, false);
        GridView gridView = (GridView) view.findViewById(R.id.gv);
        gridView.setAdapter(new ImageAdapter(this.getContext()));
        getImages();
        return view;
    }

    public void getImages() {
        File file = new File(android.os.Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES));
        if (file.isDirectory()) {
            files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                f.add(files[i].getAbsolutePath());
            }
        }
    }

    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context context) {
            mContext = context;
        }

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                //if it's not recycled, initialize some atributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new ViewGroup.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setImageResource(Integer.parseInt(f.get(position)));
            return imageView;
        }
    }
} 

But When I run the app it shows nothing in Gridview from the folder named Pictures which contains the images. I Want to show the image in full screen when the user clicks on any image in grid view. Can go with any different method too. Please help me to create the gallery for my app. Answers are really welcomed.

CodeUpdate I used this code to check file.exists :

public void getImages() {
        File file = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)));
        if (file.exists()){
            Toast.makeText(this.getContext(), "Yep It's there", Toast.LENGTH_LONG).show();
            files = file.listFiles();
            if (files !=null) {
                for (int i = 0; i < files.length; i++) {
                    //String[] FileNames= new String[files.length];
                    //FileNames[i]=files[i].getName();
                    //ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this.getContext(), android.R.layout.simple_list_item_1, FileNames);
                    //listView.setAdapter(arrayAdapter);
                    f.add(files[i].getAbsolutePath());
                }
            } else {
                Toast.makeText(this.getContext(), "error", Toast.LENGTH_SHORT).show();
            }
        }
    } 

Update I Need to add subfolders too in the Gallery and And need to upgrade from grid to recyclerview. I need to show images onclick on full screen but on other activities instead in same fragment with popup image viewer.


Solution

  • You can use this liabrary to import a simple gallery without need of much code:

    IMAGE GALLERY

    https://github.com/lawloretienne/ImageGallery/blob/master/README.md

    Sample Usage

    Intent intent = new Intent(MainActivity.this, ImageGalleryActivity.class);
    
    String[] images = getResources().getStringArray(R.array.unsplash_images);
            Bundle bundle = new Bundle();
            bundle.putStringArrayList(ImageGalleryActivity.KEY_IMAGES, new ArrayList<>(Arrays.asList(images)));
            bundle.putString(ImageGalleryActivity.KEY_TITLE, "Unsplash Images");
            intent.putExtras(bundle);
    
    startActivity(intent); 
    

    ZGallery

    https://github.com/mzelzoghbi/ZGallery

    Sample Usage

    ZGallery.with(this, /*your string arraylist of image urls*/)
                    .setToolbarTitleColor(ZColor.WHITE) // toolbar title color
                    .setGalleryBackgroundColor(ZColor.WHITE) // activity background color
                    .setToolbarColorResId(R.color.colorPrimary) // toolbar color
                    .setTitle("Zak Gallery") // toolbar title
                    .show();