0

我正在实现我自己的SearchRecentSuggestionsProvider并且一切正常,除了一件事:我无法让设备搜索显示结果的图标。我想显示应用程序数据文件夹中的图像(位于/{sdcard}/Android/data/package_name/files/

根据文档,它可以通过使用来实现SearchManager.SUGGEST_COLUMN_ICON_1,并且它显然支持多种方案,ContentResolver.SCHEME_FILE包括file. 这是官方文档的引用:

建议光标的列名。可选的。如果您的光标包含此列,则所有建议都将以包含两个小图标空间的格式提供,一个在左侧,一个在每个建议的右侧。列中的数据必须是可绘制对象的资源 ID,或者是以下格式之一的 URI:

内容 ( SCHEME_CONTENT)

android.resource ( SCHEME_ANDROID_RESOURCE)

档案 ( SCHEME_FILE)

我已经尝试了许多显而易见的事情,包括手动创建文件 URI 和使用Uri.Builder(). 这些都不起作用。

我还发现有人在 Google 网上论坛上询问同样的问题,但遗憾的是没有得到答复:https ://groups.google.com/forum/#!topic/android-developers/MJj7GIaONjc

有没有人有任何经验让设备搜索显示来自设备的本地图像?

更新(12 月 15 日): 我刚刚尝试使用ContentProviderwith aSearchView作为可搜索信息,它的工作原理与预期完全一致 - 包括封面艺术图像。尽管如此,全球搜索并没有显示它......

4

2 回答 2

3

我遇到了类似的问题,无法使其他解决方案起作用。最后,我用一个包含我在 query() 中需要的数据的新光标替换了光标。

public class RecentQueriesProvider extends SearchRecentSuggestionsProvider {
    public final static String AUTHORITY = "com.package.RecentQueriesProvider";
    public final static int MODE = DATABASE_MODE_QUERIES;

    public RecentQueriesProvider() {
        setupSuggestions(AUTHORITY, MODE);
    }

    // We override query() to replace the history icon in the recent searches suggestions. We create a new cursor
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Cursor superCursor = super.query(uri, projection, selection, selectionArgs, sortOrder);
        Uri iconUri = Uri.parse("android.resource://" + getContext().getPackageName() + "/drawable/ic_action_time");
        MatrixCursor newCursor = new MatrixCursor(superCursor.getColumnNames());
        superCursor.moveToFirst();
        while (superCursor.moveToNext()){
            newCursor.addRow(new Object[]{
                    superCursor.getInt(superCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_FORMAT)),
                    iconUri,
                    superCursor.getString(superCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1)),
                    superCursor.getString(superCursor.getColumnIndex("suggest_intent_query")),
                    superCursor.getInt(superCursor.getColumnIndex("_id"))
            });
        }
       return newCursor;
    }
}
于 2014-05-16T14:52:53.167 回答
2

一种方法是从 复制源代码android.content.SearchRecentSuggestionsProvider,将其放在扩展 ContentProvider 的类中,然后自定义setupSuggestions(String, int). 具体来说,你会改变这个:

Uri uriFile = Uri.fromFile(new File("path/to/file"));

mSuggestionProjection = new String [] {
        "0 AS " + SearchManager.SUGGEST_COLUMN_FORMAT,

        // Here, you would return a file uri: 'uriFile'
        "'android.resource://system/"
                + com.android.internal.R.drawable.ic_menu_recent_history + "' AS "
                        + SearchManager.SUGGEST_COLUMN_ICON_1,
        "display1 AS " + SearchManager.SUGGEST_COLUMN_TEXT_1,
        "query AS " + SearchManager.SUGGEST_COLUMN_QUERY,
        "_id"
};

我更喜欢以下内容。扩展SearchRecentSuggestionsProvider和覆盖该query(...)方法。在这里,拦截SearchManager.SUGGEST_URI_PATH_QUERY并返回一个游标。

public class SearchSuggestionProvider extends SearchRecentSuggestionsProvider {

    private UriMatcher matcher;

    private static final int URI_MATCH_SUGGEST = 1;

    public SearchSuggestionProvider() {
        super();

        matcher = new UriMatcher(UriMatcher.NO_MATCH);

        // Add uri to return URI_MATCH_SUGGEST
        matcher.addURI(SearchSuggestionProvider.class.getName(), 
                         SearchManager.SUGGEST_URI_PATH_QUERY, URI_MATCH_SUGGEST);

        setupSuggestions(SearchSuggestionProvider.class.getName(), 
                                                           DATABASE_MODE_QUERIES);
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, 
                                      String[] selectionArgs, String sortOrder) {

        // special case for actual suggestions (from search manager)
        if (matcher.match(uri) == URI_MATCH_SUGGEST) {

            // File to use
            File f = new File("/path/to/file");

            Uri uriFile = Uri.fromFile(f);

            final String[] PROJECTION = new String[] {
                "_id", 
                "display1 AS " + SearchManager.SUGGEST_COLUMN_TEXT_1,                
                "query AS " + SearchManager.SUGGEST_COLUMN_QUERY,
                "'" + uriFile + "'" + " AS " + SearchManager.SUGGEST_COLUMN_ICON_1,
            };

            final Uri URI = Uri.parse("content://" + 
                        SearchSuggestionProvider.class.getName() + "/suggestions");

            // return cursor
            return getContext().getContentResolver().query(
                URI, 
                PROJECTION, 
                "display1 LIKE ?", 
                new String[] {selectionArgs[0] + "%"}, 
                "date DESC");          
        }

        // Let super method handle the query if the check fails
        return super.query(uri, projection, selection, selectionArgs, sortOrder);
    }
}
于 2013-11-16T18:15:47.667 回答