【问题标题】:Open file link from android browser in my app在我的应用程序中从 android 浏览器打开文件链接
【发布时间】:2013-07-04 18:59:33
【问题描述】:

我希望能够以通常的方式运行标准的 android 浏览器,导航到带有文件链接的页面,例如

<a href=./myfile.gcsb>click here</a>

单击链接并让它启动我的应用程序,然后它可以根据需要处理文件。

该网页基本上可以正常工作 - 在 PC 上单击 Firefox 的链接,正如预期的那样,我可以选择保存文件,或者使用已注册的应用程序打开 .gcsb 文件类型。但不是在安卓设备上。

例如,我已经在我的应用中尝试了所有我能想到的意图过滤器版本

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="http" android:host="*" android:mimeType="application/*" android:pathPattern=".*\\.gcsb" />
</intent-filter>

并且它们都不会在单击浏览器中的链接时启动应用程序。

我知道有很多关于这个的帖子,但到目前为止没有一个可以帮助我解决这个问题。任何建议都非常感激。 (运行 Android OS 2.2)。

【问题讨论】:

  • 这应该会有所帮助:richardleggett.co.uk/blog/2013/01/26/…
  • 是的,谢谢,阅读。我想我已经尝试了几乎所有的方法。
  • 有趣的是,我刚刚注意到,即使我的应用程序已卸载,并且网页中包含 PDF (...,单击 PDF 链接也无济于事。我确定习惯了...。我已尝试重置所有浏览器设置。

标签: android browser href intentfilter


【解决方案1】:

排序。首先,从其他帖子看来,在同一窗口中从 android 浏览器打开链接通常存在问题。因此将 target=_blank 添加到原始文件中

<a href ...> ...

已处理(尽管请参阅最后的附带条件)。

似乎工作的意图过滤器是

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="http" android:host="*" android:pathPattern=".*\\.gcsb" />
</intent-filter>

而活动onCreate() / onRestart()中的代码是

String  action = intent.getAction();

if (!Intent.ACTION_VIEW.equals(action))
    return;

Uri     uri = intent.getData();
String  scheme = uri.getScheme();
String  name = null;

if (scheme.equals("http")) {
    List<String>
            pathSegments = uri.getPathSegments();

    if (pathSegments.size() > 0)
        name = pathSegments.get(pathSegments.size() - 1);

    URL     url = new URL(uri.toString());
    HttpURLConnection
            urlConnection = (HttpURLConnection)url.openConnection();

    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);
    urlConnection.connect();

    is = urlConnection.getInputStream();
}

else if () {
    //  Deal with other schemes (file, content) here
}

//  Should have an open input stream and a target file name/ext by here
//  Make full path to where it needs to be saved from name
//  Open output stream
//  Loop round reading a buffer full of data from input stream and writing to output stream
//  Close everything
//  Not forgetting to deal with errors along the way

这一切似乎都有效,但有两个条件:(1) 所有这些都应该在后台线程中完成,(2) target=_blank 的效果是让 android 浏览器 每次下载完成时都会打开新窗口 - 并且打开的窗口数量是有限制的。

【讨论】:

    猜你喜欢
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 2018-08-10
    • 2019-04-05
    相关资源
    最近更新 更多