使用SearchManager的SUGGEST_COLUMN_ICON_1显示本地文件

时间:2013-11-05 22:30:56

标签: android android-searchmanager

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

的图像

根据the documentation,可以使用SearchManager.SUGGEST_COLUMN_ICON_1来实现,它显然支持多种方案,包括ContentResolver.SCHEME_FILE,即file。以下是官方文档的引用:

  

建议光标的列名称。 可选。如果光标包含此列,则所有建议将以包含两个小图标空间的格式提供,一个位于左侧,另一个位于每个建议的右侧。列中的数据必须是drawable的资源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日): 我刚刚尝试使用ContentProviderSearchView作为可搜索的信息,它的工作方式与预期完全一致 - 包括封面艺术图片。不过,全球搜索还没有显示出来......

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分: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);
    }
}