-1
  public void onBindViewHolder(MyAdapter.MyViewHolde holder, int position) {
        //how to set image for loading the images for gallery 



----------


}

//and code for how to get images from gallery in main activity

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
venkatmaddy
  • 41
  • 1
  • 6
  • Hi, welcome to stack overflow. Please refer the [ask] link for more details on how to ask a question and update your question accordingly. – Jeroen Heier Aug 13 '17 at 06:54

5 Answers5

4

You have to first get all images from gallery in you activity then have to set the list of images to RecyclerView

Use below method to get all all images-

    private ArrayList<String> getAllShownImagesPath(Activity activity) {
        Uri uri;
        Cursor cursor;
        int column_index_data, column_index_folder_name;
        ArrayList<String> listOfAllImages = new ArrayList<String>();
        String absolutePathOfImage = null;
        uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        String[] projection = { MediaColumns.DATA,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME };

        cursor = activity.getContentResolver().query(uri, projection, null,
                null, null);

        column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
        column_index_folder_name = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
        while (cursor.moveToNext()) {
            absolutePathOfImage = cursor.getString(column_index_data);

            listOfAllImages.add(absolutePathOfImage);
        }
        return listOfAllImages;
    }

Now set the This list of images to RecyclerView Adapter.

Take this RecyclerView Example as an reffrence to set all gallery images.

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
  • it's working,image loading is taking more time to load.i use asyn task also but no use,it takes same timing to load. – venkatmaddy Aug 16 '17 at 04:37
  • Yes, it may take some time to load images, it depends on no. of images. But you can reduce time of rendering image by using [glide example](https://stackoverflow.com/a/39219995/6676466). Add this dependency to your build.gradle `compile 'com.github.bumptech.glide:glide:4.0.0'`. – Vishal Chhodwani Aug 16 '17 at 05:40
  • i always use glide lib only to load images,here also i used glide lib but its not working....same issue is occuring. – venkatmaddy Aug 16 '17 at 06:51
  • it means time consumed only to load images from gallery....did you try on another device that has small number of images.? I think the time consume because of only large no. of images. – Vishal Chhodwani Aug 16 '17 at 06:59
  • if i lock the screen and then unlock the screen image visible in screen fast,i thick its work in cycle of on-pause to on-resume only.it dosnt work in on-create. – venkatmaddy Aug 16 '17 at 07:12
  • If you post your related code then it will help more to understood your issue. – Vishal Chhodwani Aug 16 '17 at 07:28
2

public class GalleryFragment extends Fragment {

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

private Context context;
private RecyclerView recyclerView;
ArrayList<String> imageData;
MyAdapter adapter;

@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);
    context = getActivity();

    recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);

    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
    recyclerView.setLayoutManager(layoutManager);

    MyTask myTask = new MyTask();
    myTask.execute();

    return view;
}

private class MyTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        imageData = getAllShownImagesPath(getActivity());
        Log.e("imageData: ", String.valueOf(imageData.size()));
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        adapter = new MyAdapter(imageData, getActivity());
        recyclerView.setAdapter(adapter);

    }

}

private ArrayList<String> getAllShownImagesPath(Activity activity) {
    Uri uri;
    Cursor cursor;
    int column_index_data, column_index_folder_name;
    ArrayList<String> listOfAllImages = new ArrayList<String>();
    String absolutePathOfImage = null;
    uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

    String[] projection = {MediaStore.MediaColumns.DATA,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME};

    cursor = activity.getContentResolver().query(uri, projection, null,
            null, null);

    column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
    column_index_folder_name = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
    while (cursor.moveToNext()) {
        absolutePathOfImage = cursor.getString(column_index_data);

        listOfAllImages.add(absolutePathOfImage);
    }
    return listOfAllImages;
}

}

venkatmaddy
  • 41
  • 1
  • 6
1
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolde> {
    Context context;
    private ArrayList<String> imageData = new ArrayList<String>();


    public MyAdapter(ArrayList<String> imageData, FragmentActivity activity) {
        this.imageData = imageData;
        this.context = activity;
    }

    @Override
    public MyAdapter.MyViewHolde onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.inflate_single_image_row, parent, false);

        return new MyViewHolde(itemView);
    }

    @Override
    public void onBindViewHolder(MyAdapter.MyViewHolde holder, int position) {
        String data = imageData.get(position);
        if (data != null){
            Glide.with(context).load(data).into(holder.singleImageView);

        }else {
            Toast.makeText(context, "Images Empty", Toast.LENGTH_SHORT).show();
        }


    }

    @Override
    public int getItemCount() {
        return imageData.size();
    }

    public class MyViewHolde extends RecyclerView.ViewHolder {
        ImageView singleImageView;

        public MyViewHolde(View itemView) {
            super(itemView);
            singleImageView = (ImageView) itemView.findViewById(R.id.singleImageView);
        }
    }
}
venkatmaddy
  • 41
  • 1
  • 6
0

This Get list of photo galleries on Android will help you.. One thing i want to add here is you should add the gallery retrieving data inside a AsyncTask or Thread. It is not good practice to fetch gallery data in UI thread because if gallery images are huge than you may get ANR due to processing in UI tread.

-1
     //Get all image          
      private void getImageData() {
                        Uri uri= MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) 
                {
                            Cursor cursor= getContentResolver().query(uri
                                                                     ,null
                                                                     ,null
                                                                     ,null);
                            if (cursor != null && cursor.moveToFirst())
                            {
                                do{
                                    String path=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                                    String date
                                    =cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN));
                                    String imagename
                                    =cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
                                    Image_ModelClass modelClass=new Image_ModelClass(path,date,imagename);
                                    image_list.add(modelClass);
                                }while (cursor.moveToNext());
                            }
                            cursor.close();
                        }
        // Model Class
            public class Image_ModelClass implements Serializable {
                String Path;
                String Date;
                String ImageName;
            
                public Image_ModelClass(String path, String date, String imageName) {
                    Path = path;
                    Date = date;
                    ImageName = imageName;
                }
            
                public String getPath() {
                    return Path;
                }
            
                public void setPath(String path) {
                    Path = path;
                }
            
                public String getDate() {
                    return Date;
                }
            
                public void setDate(String date) {
                    Date = date;
                }
            
                public String getImageName() {
                    return ImageName;
                }
            
                public void setImageName(String imageName) {
                    ImageName = imageName;
                }
            }
//set image using recycler
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class image_adapter extends RecyclerView.Adapter<image_adapter.UserHolder> {
    Context context;
    ArrayList<Image_Model>image_list;

    public image_adapter(Context context, ArrayList<Image_Model> image_list) {
        this.context = context;
        this.image_list = image_list;
    }

    @NonNull
    @Override
    public image_adapter.UserHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.image_item,parent,false);
        return new UserHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull image_adapter.UserHolder holder, int position) {
        Image_Model model=image_list.get(position);
        Glide
                .with(context)
                .load(model.getPath())
                .centerCrop()
                .into(holder.imageView);
    }

    @Override
    public int getItemCount() {
        return image_list.size();
    }

    public class UserHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        public UserHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.Item_image);
        }
    }
}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 21 '23 at 23:43