Create custom gallery in android — Pictures gallery
Hello every body .
It’s my third post in Medium and i want to write about another challenge in android development about images gallery contain all of device folder’s which has picture and also show all images and folder’s sub items.
So to fetch device all gallery first need to grant read storage permission and use MediaStore and ContentResolver to fetch device images .
We need to do operation in another thread and send result with Callback using interface . So let create interface :
public interface FetchListener<T> {
void onFetch(T result);
}
Interface is generic to use for any data model . Picture , music , video and folder may have different result and in this article we learn fetching images and in another post i will wrote about fetch music and videos.
Now its time to fetch folders which contain image as a sub item.
FileManager.java:
As method written we must to do process in another thread and get result in Callback after done .
We use Cursor to fetch data which has projection as parameter . Projection define what field we need and in above sample we just get BUCKET_ID(for folders id) , DATA(path of image) and file DISPLAY_NAME as folder name .
Also we add All for show all folders and show at the top of list and set -1 as id to detect in adapter for handling sub items.
now folders listed and lets get folder items(images) :
Description available at above code .
Now you can create your gallery and convert code to Kotlin or replace Coroutines or Rx-java instead of thread .
I try to write music and video gallery in another posts .
Good luck.