我如何获得一个开放的,以我的资产的图像,将工作的SearchManager.SUGGEST_COLUMN_ICON_1列? [英] How do I get a Uri to an image in my Assets that will work for the SearchManager.SUGGEST_COLUMN_ICON_1 column?
问题描述
我已经成功地将我的应用程序的国家搜索到全局搜索功能,现在我想旁边显示的搜索建议每个国家的国旗。搜索在我的应用程序以这种方式工作,但我当然有列表和视图结合自己的控制权。所以我知道的标志都在那里,我可以使用他们在我的应用程序的其余部分。
麻烦来当我尝试在我的资产提供一个开放的,以.gif文件。根据搜索文档与键列的值 SearchManager.SUGGEST_COLUMN_ICON_1
应该是一个开放的我们的形象。
下面是什么code样子。为响应的ContentProvider
方法公开光标查询(URI URI,字符串[]投影,弦乐选择,字符串[] selectionArgs两个,字符串排序顺序)
我创建一个 MatrixCursor
的映射列从我的国家数据库,那些由搜索工具必需的。国名显示了罚款,我可以选择它们,并正确地在我的申请作出答复。
我试图形成开放的三种不同的方式:
//字符串flagUri =文件:/// android_asset /+ flagPath;
//字符串flagUri =文件:///资产/+ flagPath;
字符串flagUri =android.resource://com.lesliesoftware.worldinfo.WorldInfoActivity/assets/+ flagPath;
columnValues.add(flagUri);
它们都导致了同样的事情 - 一个接一个建议,我可以用空字符串的值,让我的应用程序图标
有一个开放的,将工作?我怎样才能获得该国国旗图标旁边的搜索建议?
由于伊恩
的全部源代码:
私人光标搜索(查询字符串,INT限制){
查询= query.toLowerCase();
的String [] requestedColumns =新的String [] {
BaseColumns._ID,
SearchManager.SUGGEST_COLUMN_TEXT_1,
SearchManager.SUGGEST_COLUMN_ICON_1,
};
的String [] queryColumns =新的String [] {
WorldInfoDatabaseAdapter.KEY_ROWID,
WorldInfoDatabaseAdapter.KEY_COUNTRYNAME,
WorldInfoDatabaseAdapter.KEY_COUNTRY code
};
返回packageResults(查询,requestedColumns,queryColumns,限制);
}
私人光标packageResults(查询字符串,字符串[] requestedColumns,字符串[] queryMappedColumns,INT限制){
如果(requestedColumns.length!= queryMappedColumns.length)
抛出新抛出:IllegalArgumentException(内部错误:请求的列没有映射到查询栏);
MatrixCursor结果=新MatrixCursor(requestedColumns);
//查询国家列表返回列:KEY_ROWID,KEY_COUNTRYNAME,KEY_COUNTRY code
光标DBRESULTS = myDbHelper.getCountryList(查询);
//验证查询列可用
对于(INT指数= 0;指数< queryMappedColumns.length;指数++){
INT COL = dbResults.getColumnIndex(queryMappedColumns [指数]);
如果(COL == -1)
抛出新抛出:IllegalArgumentException(内部错误:请求的列'+
queryMappedColumns [指数] +不是从数据库返回的。);
}
//遍历数据库结果建立请求的结果
INT rowCount时= 0;
而(dbResults.moveToNext()及和放大器; rowCount等<限制){
矢量<串GT; columnValues =新矢量<串GT; ();
对于(INT指数= 0;指数< requestedColumns.length;指数++){
如果(requestedColumns [指数] .compareTo(SearchManager.SUGGEST_COLUMN_ICON_1)== 0){
字符串flagPath =标志/小/+ dbResults.getString(
dbResults.getColumnIndexOrThrow(queryMappedColumns [指数]))
+-flag.gif;
//字符串flagUri =文件:/// android_asset /+ flagPath;
//字符串flagUri =文件:///资产/+ flagPath;
字符串flagUri =android.resource://com.lesliesoftware.worldinfo.WorldInfoActivity/assets/+ flagPath;
columnValues.add(flagUri);
} 其他 {
//从数据库中添加映射查询列值
字符串COLVALUE = dbResults.getString(dbResults.getColumnIndexOrThrow(queryMappedColumns [指数]));
columnValues.add(COLVALUE);
}
}
results.addRow(columnValues);
rowCount等++;
}
返回结果;
}
编辑: 我曾尝试过其他的变化包括从移动资产将图像的原始文件夹。毫无效果。这里是URI的我尝试:
flagUriStr =android.resource://com.lesliesoftware.worldinfo/raw/flags/small/+
国家code +-flag.gif;
flagUriStr =android.resource://com.lesliesoftware.worldinfo/assets/flags/small/+
国家code +-flag.gif;
flagUriStr =android.resource://com.lesliesoftware.worldinfo/assets/flags/small/+
国家code +-flag;
flagUriStr =android.resource://com.lesliesoftware.worldinfo/raw/+
国家code +-flag.gif;
flagUriStr =android.resource://com.lesliesoftware.worldinfo/raw/+
国家code +-flag;
这是没有工作的唯一的URI是,如果我移动的测试标志到我绘制的文件夹:
flagUriStr =android.resource://com.lesliesoftware.worldinfo/+
R.drawable.small_ca_flag;
可悲的是经过一番摸索我已经达到了,你不能一个URI返回图像资源的搜索工具的结论。我所做的,而不是被移动我的标志图像资源(这样它们就不会弄乱我的应用程序的资源,我创建了标志图像库),并利用资源的URI的。所以,在我的供应商我有code,看起来像这样的一个映射数据库结果搜索结果的循环:
如果(requestedColumns [指数] .compareTo(SearchManager.SUGGEST_COLUMN_ICON_1)== 0){
//翻译全国code到一个旗帜图标的URI
字符串国code = dbResults.getString(
dbResults.getColumnIndexOrThrow(queryMappedColumns [指数]));
INT flagImageID = FlagLookup.smallFlagResourceID(国家code);
字符串flagUriStr =android.resource://com.lesliesoftware.worldinfo/+ flagImageID;
columnValues.add(flagUriStr);
}
和查找code,它是这样的:
静态公众诠释smallFlagResourceID(字符串国code){
如果(国家code == NULL ||国家code.length()== 0)
返回R.drawable.flag_small_none;
如果(国家code.equalsIgnoreCase(AA))
返回R.drawable.flag_small_aa;
否则,如果(国家code.equalsIgnoreCase(AC))
返回R.drawable.flag_small_ac;
否则,如果(国家code.equalsIgnoreCase(AE))
返回R.drawable.flag_small_ae;
...
我只是要确保我创建一个测试,验证具有标志所有国家返回预期的结果。
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 inside my app works this way but of course I have control of the list and its view binding myself. So I know the flags are all there and I can use them in the rest of my app.
The trouble comes when I try to supply a Uri to a .gif file in my Assets. According to the search documentation the value of the column with the key SearchManager.SUGGEST_COLUMN_ICON_1
should be a Uri to the image.
Below is what the code looks like. In response to the ContentProvider
method public Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
I am creating a MatrixCursor
that maps columns from my country database to those required by the search facility. The country names show up fine and I can select them and correctly respond in my application.
I have tried forming the Uri three different ways:
// String flagUri = "file:///android_asset/" + flagPath;
// String flagUri = "file:///assets/" + flagPath;
String flagUri = "android.resource://com.lesliesoftware.worldinfo.WorldInfoActivity/assets/" + flagPath;
columnValues.add (flagUri);
They all lead to the same thing - my application icon next to each suggestion which I can get by using a value of empty string.
Is there a Uri that will work? How can I get the country flag icon next to the search suggestions?
Thanks Ian
The full source:
private Cursor search (String query, int limit) {
query = query.toLowerCase ();
String[] requestedColumns = new String[] {
BaseColumns._ID,
SearchManager.SUGGEST_COLUMN_TEXT_1,
SearchManager.SUGGEST_COLUMN_ICON_1,
};
String[] queryColumns = new String[] {
WorldInfoDatabaseAdapter.KEY_ROWID,
WorldInfoDatabaseAdapter.KEY_COUNTRYNAME,
WorldInfoDatabaseAdapter.KEY_COUNTRYCODE
};
return packageResults (query, requestedColumns, queryColumns, limit);
}
private Cursor packageResults (String query, String[] requestedColumns, String[] queryMappedColumns, int limit) {
if (requestedColumns.length != queryMappedColumns.length)
throw new IllegalArgumentException ("Internal error: requested columns do not map to query columns");
MatrixCursor results = new MatrixCursor (requestedColumns);
// Query the country list returns columns: KEY_ROWID, KEY_COUNTRYNAME, KEY_COUNTRYCODE
Cursor dbResults = myDbHelper.getCountryList (query);
// Verify that the query columns are available
for (int index = 0; index < queryMappedColumns.length; index++) {
int col = dbResults.getColumnIndex (queryMappedColumns[index]);
if (col == -1)
throw new IllegalArgumentException ("Internal error: requested column '" +
queryMappedColumns[index] + "' was not returned from the database.");
}
// Loop over the database results building up the requested results
int rowCount = 0;
while (dbResults.moveToNext () && rowCount < limit) {
Vector<String> columnValues = new Vector<String> ();
for (int index = 0; index < requestedColumns.length; index++) {
if (requestedColumns[index].compareTo (SearchManager.SUGGEST_COLUMN_ICON_1) == 0) {
String flagPath = "flags/small/" + dbResults.getString (
dbResults.getColumnIndexOrThrow (queryMappedColumns[index]))
+ "-flag.gif";
// String flagUri = "file:///android_asset/" + flagPath;
// String flagUri = "file:///assets/" + flagPath;
String flagUri = "android.resource://com.lesliesoftware.worldinfo.WorldInfoActivity/assets/" + flagPath;
columnValues.add (flagUri);
} else {
// Add the mapped query column values from the database
String colValue = dbResults.getString (dbResults.getColumnIndexOrThrow (queryMappedColumns[index]));
columnValues.add (colValue);
}
}
results.addRow (columnValues);
rowCount++;
}
return results;
}
EDIT: I have tried other variations including moving the images from the assets to the raw folder. Nothing worked. Here are the uri's I tried:
flagUriStr = "android.resource://com.lesliesoftware.worldinfo/raw/flags/small/" +
countryCode + "-flag.gif";
flagUriStr = "android.resource://com.lesliesoftware.worldinfo/assets/flags/small/" +
countryCode + "-flag.gif";
flagUriStr = "android.resource://com.lesliesoftware.worldinfo/assets/flags/small/" +
countryCode + "-flag";
flagUriStr = "android.resource://com.lesliesoftware.worldinfo/raw/" +
countryCode + "-flag.gif";
flagUriStr = "android.resource://com.lesliesoftware.worldinfo/raw/" +
countryCode + "-flag";
The only uri that did work was if I moved a test flag into my drawable folder:
flagUriStr = "android.resource://com.lesliesoftware.worldinfo/" +
R.drawable.small_ca_flag;
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.
这篇关于我如何获得一个开放的,以我的资产的图像,将工作的SearchManager.SUGGEST_COLUMN_ICON_1列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!