Android media viewer cursor loader

Introduction

The android media viewer cursor loader tutorial series describes how to implement android cursor loaders to the app.

A cursor loader is a convenient way to query the MediaStore and have a Cursor returned while running in a background thread.

Get Code

The code can be found on github from the following instructions below

https://github.com/mobapptuts/media-thumbnail-viewer.git Tag

media-viewer-cursor-loader

or you can run this command

git clone https://github.com/mobapptuts/media-thumbnail-viewer.git –branch

media-viewer-cursor-loader

This video describes how to import the code from github using android studio and also how to use git tags

Steps

Use the LoaderCallbacks<Cursor> interface and implement its methods

public class MediaViewerMainActivity extends AppCompatActivity
    implements LoaderManager.LoaderCallbacks<Cursor>{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_viewer_main);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return null;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }
}

Add a member id to represent the loader request

private final static int MEDIASTORE_LOADER_ID = 0;

Initialise the loader

private void checkReadExternalStoragePermission() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {
            if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) ==
                    PackageManager.PERMISSION_GRANTED) {
                getSupportLoaderManager().initLoader(MEDIASTORE_FILES_LOADER_ID, null, this);
            } else {
                if(shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    Toast.makeText(this, "need access to sdcard to load images", Toast.LENGTH_SHORT).show();
                }
                requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_EXTERNAL_STORAGE_PERMISSION);
            }
        } else {
            getSupportLoaderManager().initLoader(MEDIASTORE_FILES_LOADER_ID, null, this);
        }
    }
@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch(requestCode) {
            case REQUEST_READ_EXTERNAL_STORAGE_PERMISSION:
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    getSupportLoaderManager().initLoader(MEDIASTORE_FILES_LOADER_ID, null, this);
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

 

Setup the query for MediaStore images & videos

@Override
   public Loader<Cursor> onCreateLoader(int id, Bundle args) {
       switch (id) {
           case MEDIASTORE_FILES_LOADER_ID:
               String[] projection = {
                       MediaStore.Files.FileColumns._ID,
                       MediaStore.Files.FileColumns.DATE_ADDED,
                       MediaStore.Files.FileColumns.MEDIA_TYPE
               };
               String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                       + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
                       + " OR "
                       + MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                       + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;
               return new CursorLoader(
                       this,
                       MediaStore.Files.getContentUri("external"),
                       projection,
                       selection,
                       null,
                       MediaStore.Files.FileColumns.DATE_ADDED + " DESC"
               );
           default:
               return null;
       }
   }

Android media viewer cursor loader summary

This android development tutorial provided an example of how to implement a CursorLoader to the android activity. The CursorLoader will be used to query the MediaStore for images and videos. Then return this information in the form of a cursor. Which will then be provided to the RecyclerView adapter.

And one of the advantages of using a CursorLoader is that the query will happen on a background thread therefore not impacting the performance of the UI thread.

 

About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>