Search code examples
androiduriandroid-contentprovider

Android ContentProvider Uri Matching


I am trying to create a content provider where the key contains forward slash "/". I searched about it for quite a while but there is no place/example illustrating it.

content://com.lily.provider/items/*

General example, which I understand: content://com.lily.provider/items/ab

What I want to do: my key is a string with "/"
content://com.lily.provider/items/a/b where a/b is the item id, which is in the same position as ab.

My question:

  1. will content://com.lily.provider/items/a/b be matched to content://com.lily.provider/items/* ? why is that?
  2. are there any work-around I could use to solve the problem

Solution

  • Will content://com.lily.provider/items/a/b be matched to content://com.lily.provider/items/* ? why is that?

    Yes, it will match. The asterisk * means "match any characters, including slashes".

    Are there any work-around I could use to solve the problem

    If you want to match known prefixes, then you can just add more entries to your URI matcher (in this order):

    • content://com.lily.provider/items/a/*
    • content://com.lily.provider/items/b/*
    • content://com.lily.provider/items/*

    If you insist on having slashes in the data, then you should URI-encode slashes that aren't being used as path separators to %2f.

    Otherwise, I'm not sure what the problem is. The "/items/a/b" URI will match your original pattern as desired, and then you can parse the path component of the URI as you wish.