How do I get a Uri to an image in my Assets that will work for the SearchManager.SUGGEST_COLUMN_ICON_1 column?

前端 未结 4 1111
臣服心动
臣服心动 2020-12-18 04:01

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.

4条回答
  •  无人及你
    2020-12-18 04:40

    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:

    @Override
    public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
            String filename = uri.getLastPathSegment();
    
            try {
                File file = new File(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);
    
                AssetFileDescriptor afd = new AssetFileDescriptor(
                            ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY | ParcelFileDescriptor.MODE_WORLD_READABLE),
                        0, AssetFileDescriptor.UNKNOWN_LENGTH);
    
                return afd;
            } catch (IOException e) {
                throw new FileNotFoundException("No asset found: " + uri);
            }
        }
    

提交回复
热议问题