Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio - prompt to open a downloaded file with any application available

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.

like image 965
A. M. Avatar asked May 06 '17 16:05

A. M.


1 Answers

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.

like image 134
lax1089 Avatar answered Oct 16 '22 08:10

lax1089