简体   繁体   中英

Open a url to a file in Android without using a browser

I am experimenting with Android development. I am making an app that will allow the user to browse files in a web service and view them. These files could be anything: text, pdf, pictures, etc.

Previously, I would download the file to external storage and then call Intent.SetDataAndType() and pass it the URL to the file. That would cause the Android device to bring up an app picker and let the user choose the appropriate method to look at the file.

But since I do not want the user to edit the file, only to look at it, it seemed silly to download a file to storage; a file that I didn't want to hang around. Since the file can be obtained by a URL, why don't I pass that as a parameter to the Intent.SetDataAndType() ?

I tried that. The first problem was that the file name was assumed to be the name of the web service call, and that seemed to be more important than the mime-type. I changed the web service to be the same name as whatever file was attempting to be downloaded. That solved that issue.

So now, the file is being opened. But it is always being opened in a web browser. I get to choose the web browser, but I would rather have another app open it.

My code looks like this:

Intent i = new Intent(Intent.ActionView);

i.SetDataAndType(Android.Net.Uri.Parse(GetUrlToFile(fileref, fileName)), mimeType);

i.SetFlags(ActivityFlags.GrantReadUriPermission);
i.SetFlags(ActivityFlags.NewTask);
i.SetFlags(ActivityFlags.ClearWhenTaskReset); // so if the app is relaunched, we don't show the display application.

StartActivity(i);

The code is in C# because I'm using Xamarin, but I don't believe that should make a difference.

I tried using StartActivity(Intent.CreateChooser(i, "Open me")); but that didn't give me any more options for choosing.

Does anyone have any ideas as to how to do this?

I have not found a way to do this yet, so I have gone through a workaround.

Instead of using a URL, I changed my app to be a Content Provider as well. Now, when I want the file opened, I create a URI that refers to the file within my app and pass that off to an Intent. When my app is contacted by this Intent, I download the file locally to my cache directory and return that.

My code has changed to this:

Intent i = new Intent(Intent.ActionView);

i.SetDataAndType(Android.Net.Uri.Parse("content://com.sample.erik.provider/files/" + id), mimeType);
i.SetFlags(ActivityFlags.GrantReadUriPermission);
i.SetFlags(ActivityFlags.NewTask);
i.SetFlags(ActivityFlags.ClearWhenTaskReset); // so if the app is relaunched, we don't show the display application.

StartActivity(i);

Then, I have my own content provider which does most of the work in OpenFile()

public override ParcelFileDescriptor OpenFile(Android.Net.Uri uri, string mode)
{
    switch (sUriMatcher.Match(uri))
    {
        case FILE_ID:
            if (mode != "r")
                throw new Java.Lang.UnsupportedOperationException("Do not support write access: " + uri);

            String id = uri.LastPathSegment;
            Java.IO.File file = new Java.IO.File(Application.Context.CacheDir, id);

            DownloadToFile(file, id);

            return ParcelFileDescriptor.Open(file, ParcelFileMode.ReadOnly);
        default:
            throw new Java.Lang.IllegalArgumentException("Unknown Uri: " + uri);

    }
}

It is not my original plan, but this way seems to work quite well and meets my needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM