-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,352
  • 21
  • 80
  • 110
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

4 Answers4

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,529
  • 5
  • 25
  • 37
  • 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.