I am trying to load images from my phone into my recyclerview.
So far, I have created a recyclerview.adapter, and a gridlayoutmanager, and attached them to my recyclerview. I can successfully retrieve full paths of the images and add them to the adapter using an async task.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
settingUp();
run();
}
public void settingUp(){
//setting up recycler view
recyclerView = findViewById(R.id.imageGallery);
recyclerView.setHasFixedSize(true);
//setting up recyclerview layout manager
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),2);
recyclerView.setLayoutManager(layoutManager);
//setting up recyclerview adapter
adapter = getInstance(getApplicationContext());
recyclerView.setAdapter(adapter);
// instance of load task
loadTask = new LoadTask();
}
private void run() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
else
{
loadTask.execute();
}
}
/**
* Loads the images into the cursor using a background thread
*/
private class LoadTask extends AsyncTask<Void,Void, Cursor>
{
// images are loaded into the cursor here, in the background.
@Override
protected Cursor doInBackground(Void... voids) {
loadImagesIntoCursor();
return imageCursor;
}
// this method runs after doInBackground finishes
@Override
protected void onPostExecute(Cursor cursor) {
transferImagesToAdapter(cursor);
}
}
//*********************************************************************************************************
/**
* Loads all the Images from external storage into the cursor.
*/
private void loadImagesIntoCursor()
{
imageCursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media.DATA}, null, null,
MediaStore.Images.Media.DATE_ADDED + " DESC");
}
private void transferImagesToAdapter(Cursor imageCursor)
{
String path;
if(imageCursor != null)
{
imageCursor.moveToFirst();
while (!imageCursor.isAfterLast()){
path = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
adapter.add(path);
imageCursor.moveToNext();
}
imageCursor.close();
}
}
/** MY ADAPTER CLASS **/
class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder>{
private ArrayList<String> images;
//private static ImageAdapter singleton = null;
Context context;
public ImageAdapter(Context context, ArrayList<String> paths)
{
images = paths;
this.context = context;
}
public ImageAdapter(Context context)
{
this.context = context;
}
//--------- OVERRIDE METHODS ---------------------------------------------
@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_layout,parent);
return new ImageViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
// load image into image holder view using picasso library
Picasso.with(context).load(images.get(position)).into(holder.image);
}
@Override
public int getItemCount() {
return images.size();
}
//------------------------------------------------------------------------
public void add(String path){
this.images.add(path);
}
//------------------------------------------------------------------------
public class ImageViewHolder extends RecyclerView.ViewHolder{
protected ImageView image;
public ImageViewHolder(View view){
super(view);
this.image = view.findViewById(R.id.img);
}
}
}
However, after adding the imagepaths I have no clue as to what trigger the onCreateViewHolder and onBindViewHolder methods in the adapter which will display my images in the recyclerview. Any help please!