Skip to content Skip to sidebar Skip to footer

How Do I Get A Uri To An Image In My Assets That Will Work For The Searchmanager.suggest_column_icon_1 Column?

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. Search in

Solution 1:

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 uriStringcountryCode= dbResults.getString (
                        dbResults.getColumnIndexOrThrow (queryMappedColumns[index]));

                intflagImageID= FlagLookup.smallFlagResourceID (countryCode);
                StringflagUriStr="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;
    elseif (countryCode.equalsIgnoreCase ("ac"))
        return R.drawable.flag_small_ac;
    elseif (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.

Solution 2:

You first create an AssetProvider. We will later create uris that will be handled by this class.

publicclassAssetsProviderextendsContentProvider {

    privateAssetManager assetManager;
    publicstatic final UriCONTENT_URI = 
            Uri.parse("content://com.example.assets");

    @Overridepublic int delete(Uri arg0, String arg1, String[] arg2) { return0; }

    @OverridepublicStringgetType(Uri uri) { returnnull; }

    @OverridepublicUriinsert(Uri uri, ContentValues values) { returnnull; }

    @OverridepublicbooleanonCreate() {
        assetManager = getContext().getAssets();
        returntrue;
    }

    @OverridepublicCursorquery(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { returnnull; }

    @Overridepublic int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return0; }

    @OverridepublicAssetFileDescriptoropenAssetFile(Uri uri, String mode) throws FileNotFoundException {
        String path = uri.getPath().substring(1);
        try {
            AssetFileDescriptor afd = assetManager.openFd(path);
            return afd;
        } catch (IOException e) {
            thrownewFileNotFoundException("No asset found: " + uri, e);
        }
    }
}

Lot's of boilerplate in there. The essential method is of course the openAssetFile that justs translates the path of the uri passed to it into a AssetFileDescriptor. In order for Android to be able to pass URIs to this provider, remember to include this in your AndroidManifest.xml file inside the application-tag:

<providerandroid:name=".AssetsProvider"android:authorities="com.example.assets" />

Now, in your search provider you can create an URI like this:

content://com.example.assets/my_folder_inside_assets/myfile.png

Solution 3:

I was able to refer images from drawable in SearchRecentSuggestionsProvider using following uri,

"android.resource://your.package.here/drawable/image_name

Solution 4:

Just wanted to add to vidstige's answer: I wanted to serve files that I had downloaded to my temporary cache directory earlier, so I can serve external image thumbnails to the SearchView suggestions from URLs in my search ContentProvider. I used this function instead:

@Overridepublic AssetFileDescriptor openAssetFile(Uri uri, String mode)throws FileNotFoundException {
        Stringfilename= uri.getLastPathSegment();

        try {
            Filefile=newFile(getContext().getCacheDir(), filename);

            // image downloading has to be done in the search results provider, since it's not on the UI thread like this guy.//downloadAndSaveFile("http://urdomain/urfile.png", filename);AssetFileDescriptorafd=newAssetFileDescriptor(
                        ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY | ParcelFileDescriptor.MODE_WORLD_READABLE),
                    0, AssetFileDescriptor.UNKNOWN_LENGTH);

            return afd;
        } catch (IOException e) {
            thrownewFileNotFoundException("No asset found: " + uri);
        }
    }

Post a Comment for "How Do I Get A Uri To An Image In My Assets That Will Work For The Searchmanager.suggest_column_icon_1 Column?"