I have an application which receives urls from a server, using Firebase. When the url is received, the file at that url is automatically saved on the local storage (they are gpx files).
But after this, I want my application to prompt the user to open the downloaded file with whatever navigation application they have installed on their phone. Something similar to when you go to the file manager yourself, click on a file, and the system shows you all applications which could open that file, and lets you chose (or opens it directly with your default setting, if you have one).
I have not been able to find anything about this, and searches containing "android studio" and "open file" show mostly tutorials about how to manage the source files themselves, so they are not helpful at all.
You could do something like:
String mimeType = myMime.getMimeTypeFromExtension(fileExt(getFile()).substring(1));
newIntent.setDataAndType(Uri.fromFile(getFile()),mimeType);
private String fileExt(String url) {
if (url.indexOf("?") > -1) {
url = url.substring(0, url.indexOf("?"));
}
if (url.lastIndexOf(".") == -1) {
return null;
} else {
String ext = url.substring(url.lastIndexOf(".") + 1);
if (ext.indexOf("%") > -1) {
ext = ext.substring(0, ext.indexOf("%"));
}
if (ext.indexOf("/") > -1) {
ext = ext.substring(0, ext.indexOf("/"));
}
return ext.toLowerCase();
}
}
This allows you to get the type based on file extension. Then just startActivity
passing in the newIntent
. This should achieve what you are looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With