How do I get a Uri to an image in my Assets that will work for the SearchManager.SUGGEST_COLUMN_ICON_1 column?

前端 未结 4 1110
臣服心动
臣服心动 2020-12-18 04:01

I have successfully integrated my app\'s country search into the global search facility and now I am trying to display each country\'s flag next to the search suggestions.

4条回答
  •  情话喂你
    2020-12-18 04:38

    Sadly after much searching around I have reached the conclusion that you cannot return a uri to an image asset to the search facility. What I did instead was move my flag images to the resources (so they do not clutter up my app's resources I created a library for the flag images) and use resource uri's. So, in my provider I have code that looks like this in the loop that maps database results to search results:

                if (requestedColumns[index].compareTo (SearchManager.SUGGEST_COLUMN_ICON_1) == 0)  {
                    //  Translate the country code into a flag icon uri
                    String countryCode = dbResults.getString (
                            dbResults.getColumnIndexOrThrow (queryMappedColumns[index]));
    
                    int flagImageID = FlagLookup.smallFlagResourceID (countryCode);
                    String flagUriStr = "android.resource://com.lesliesoftware.worldinfo/" + flagImageID;
                    columnValues.add (flagUriStr);
                }  
    

    and look up code that looks like this:

    static public int smallFlagResourceID (String countryCode)  {
        if (countryCode == null || countryCode.length () == 0)
            return R.drawable.flag_small_none;
    
        if (countryCode.equalsIgnoreCase ("aa"))
            return R.drawable.flag_small_aa;
        else if (countryCode.equalsIgnoreCase ("ac"))
            return R.drawable.flag_small_ac;
        else if (countryCode.equalsIgnoreCase ("ae"))
            return R.drawable.flag_small_ae;
        ...
    

    I will just have to make sure I create a test that verifies that all countries that have flags returns the expected results.

提交回复
热议问题