问题
I'm trying to create a Dialog, which will display a list of availible applications to open a given filetype.
I've been looking at some question here on stackoverflow that work with the same issue, but I get lost due to lacking answer. In particular I've been looking at this question:
In Android, How can I display an application selector based on file type?
My follow-up question is then:
- What to do with the
List<ResolveInfo>
? - How can I display the availible applications with name and icon?
- When the user selects an application, how can I start it based on the ResolveInfo of the selected item?
回答1:
I found a solution, that gives me a full list of applications that can open a given mimetype. It's not the best, as I would like the list to be more specific and just a few applications (e.g. if it was an URL I wanted to open only browsers would show up) but serves the purpose.
Here is what I did:
File file = new File( "myFileLocation.txt" );
String extension = MimeTypeMap.getFileExtensionFromUrl( file.getPath() );
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
Intent intent = new Intent();
intent.setAction( Intent.ACTION_VIEW );
intent.setDataAndType( Uri.fromFile( file ), type );
context.startActivity( intent );
来源:https://stackoverflow.com/questions/7635764/application-selection-dialog-based-on-filetype