简体   繁体   English

在不使用浏览器的情况下在Android中打开文件的网址

[英]Open a url to a file in Android without using a browser

I am experimenting with Android development. 我正在尝试Android开发。 I am making an app that will allow the user to browse files in a web service and view them. 我正在制作一个应用程序,它将允许用户浏览Web服务中的文件并查看它们。 These files could be anything: text, pdf, pictures, etc. 这些文件可以是任何文件:文本,pdf,图片等。

Previously, I would download the file to external storage and then call Intent.SetDataAndType() and pass it the URL to the file. 以前,我将文件下载到外部存储,然后调用Intent.SetDataAndType()并将URL传递给文件。 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. 这将导致Android设备启动应用选择器,并让用户选择适当的方法来查看文件。

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() ? 由于可以通过URL获取文件,所以为什么不将其作为参数传递给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. 第一个问题是,假定文件名是Web服务调用的名称,并且它似乎比mime类型更重要。 I changed the web service to be the same name as whatever file was attempting to be downloaded. 我将Web服务更改为与试图下载的任何文件相同的名称。 That solved that issue. 那解决了那个问题。

So now, the file is being opened. 因此,现在正在打开文件。 But it is always being opened in a web browser. 但是它总是在Web浏览器中打开。 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. 代码在C#中,因为我使用的是Xamarin,但我认为这不会有所作为。

I tried using StartActivity(Intent.CreateChooser(i, "Open me")); 我尝试使用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. 我没有使用URL,而是将我的应用程序也更改为内容提供商。 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. 现在,当我想要打开文件时,我创建了一个引用我的应用程序中文件的URI,并将其传递给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() 然后,我有自己的内容提供程序,该程序在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. 这不是我最初的计划,但是这种方式似乎可以很好地满足我的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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