Using web URL for SUGGEST_COLUMN_ICON_1 for search suggestions

I have a SearchManager installation where a drop-down list of offers will be displayed by user type. Results are received from my server (http). I would like to display an icon with each option (if the file actually exists).

Looking at the documents, I see that the parameters for the constant column SUGGEST_COLUMN_ICON_1 allow the use of the following parameters:

 Column name for suggestions cursor. Optional. If your cursor includes this column, then all suggestions will be provided in a format that includes space for two small icons, one at the left and one at the right of each suggestion. The data in the column must be a resource ID of a drawable, or a URI in one of the following formats: content (SCHEME_CONTENT) android.resource (SCHEME_ANDROID_RESOURCE) file (SCHEME_FILE) 

All I have is a URL. Which option is best for me?

Here is the class where I do this:

 public class MyCustomSuggestionProvider extends SearchRecentSuggestionsProvider { public static final String AUTHORITY = "---.MyCustomSuggestionProvider"; public static final int MODE = DATABASE_MODE_QUERIES; private final static String[] COLUMN_NAMES = {BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1, SearchManager.SUGGEST_COLUMN_TEXT_2, SearchManager.SUGGEST_COLUMN_QUERY, SearchManager.SUGGEST_COLUMN_INTENT_DATA, SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA, SearchManager.SUGGEST_COLUMN_ICON_1, SearchManager.SUGGEST_COLUMN_INTENT_ACTION}; public MyCustomSuggestionProvider() { setupSuggestions(AUTHORITY, MODE); } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor recentCursor = super.query(uri, projection, selection, selectionArgs, sortOrder); String query = selectionArgs[0]; if (query == null || query.length() < 3) { return recentCursor; } final MatrixCursor customCursor = new MatrixCursor(COLUMN_NAMES); // Get web results from Retrofit Library List<TheProfile> suggestions = RestClient.get().getCustomSearch(query, MyApp.getUserId()); for (TheProfile suggestion : suggestions) { Uri searchIconUri = Uri.parse("http:/---/profile_images/" + String.valueOf(suggestion.id) + ".png"); try { customCursor.addRow(new Object[]{ suggestion.id, suggestion.profile, suggestion.subcategory, suggestion.profile, suggestion.profile, suggestion.subcategory, searchIconUri, "android.intent.action.SEARCH"}); } catch (Exception e) { e.printStackTrace(); } } return customCursor; } } 
+8
android url uri android-searchmanager search-suggestion
source share
2 answers

For those who are still looking for the answer to this question, like me. This is very similar to my code, so I decided to share it. I spent a lot of time collecting all this. Perhaps I will save something. First of all, you need the Glide library .

Add it to the build.gradle file of the application:

 repositories { mavenCentral() // jcenter() works as well because it pulls from Maven Central } dependencies { compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.android.support:support-v4:19.1.0' } 

Now let's make some changes to the code from the question (in the MyCustomSuggestionProvider class): Put it in your for (TheProfile suggestion : suggestions) {

 FutureTarget<File> futureTarget = Glide .with(getContext().getApplicationContext()) .load(searchIcon) .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL); File cacheFile = futureTarget.get(); Uri searchIconUri = Uri.fromFile(cacheFile); 

Pay attention to this line of code: .with(getContext().getApplicationContext()) It is very important to get the application context, not just the context, since we will not show bmp in ImageView. The official Glide documentation for this type of use of Glide.

And you can call:

 // do things with bitmap and then release when finished: Glide.clear(futureTarget); 
+4
source share
  • Collect all the files that you intend to use as icons. They are probably on your server; You need to embed them in your application.

  • If they are not in .PNG format, convert them to .PNG format. Scale them to the size necessary for display in the application.

  • Add them to your Android project in the / res / drawable -mdpi folder. Putting them in a folder specific to mdpi will scale them the same size in different device resolutions.

  • The first part of the icon code has a return URI for the SearchManager . Use the scheme "android.resource" in the format:

     android.resource://<package-name>/<resource-type>/<resource-name> 

    For example, you can create a final URI for each icon. Here is an example URI that I used in my project for /res/drawable-mdpi/ic_autocomplete_1.png :

     private final Uri searchIconUri = Uri.parse("android.resource://com.mycompany.android/drawable/ic_autocomplete_1"); 
  • As you look at your sentences, determine which icon is needed, for example, using the switch , and put this URI in your string object, as you have in your code.

+2
source share

All Articles