Android Studio - an invitation to open the downloaded file with any available application

I have an application that receives URLs from a server using Firebase. When the URL is received, the file in this URL is automatically saved in local storage (they are gpx files).

But after that, I want my application to prompt the user to open the downloaded file using any navigation application that they installed on their phone. Something similar to when you go to the file manager yourself, click on the file, and the system will show you all the applications that can open this file and allow you to select (or open it directly with the default settings, if you have it there is).

I couldn’t find anything, and searches containing "android studio" and "open file" mainly show tutorials on how to manage the source files, so they don’t help at all.

+6
source share
1 answer

You can 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 a type based on the file extension. Then just startActivitypassing in newIntent. This should provide what you are looking for.

+1
source

Source: https://habr.com/ru/post/1017055/


All Articles