【问题标题】:Android Download file from a linkAndroid 从链接下载文件
【发布时间】:2016-02-16 18:26:34
【问题描述】:

在我为 Android 应用实施了一个解决方案以发布到网络服务器并验证 Google 订单之后,然后发布一个下载链接。现在我正在尝试为应用程序编写代码以从thankyou.php页面读取链接

<a href="http://domain.com/218348214.dat">Download File</a>

该文件是“.DAT”扩展名,来自某个链接。应用程序应在新页面中检索链接并让客户下载文件。

【问题讨论】:

    标签: android android-download-manager download-manager


    【解决方案1】:

    在清单文件中,您需要添加一个意图过滤器:

        <activity
            android:name="Downloader">
            <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:host="domain.com"
                    android:scheme="http" />
            </intent-filter>
        </activity>
    

    然后在“下载”活动的 onCreate 中:

    public class Download extends Activity {
    
    private String filename;
    
    @Override
    protected void onCreate (final Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(<your layout file>);
        final Intent intent = getIntent ();
        final Uri data = intent.getData ();
        if (data != null) {
            final List<String> pathSegments = data.getPathSegments ();
            fileName = pathSegments.get (pathSegments.size () - 1);
        }
    }
    

    然后在下载按钮的点击处理程序中,您可以使用视图意图来充当 android 中的链接。

    button.setOnClickListener (new View.OnClickListener () {
            @Override public void onClick (final View v) {
                Uri intentUri = Uri.parse("http://domain.com/" + filename);
    
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(intentUri);
                startActivity(intent);
            }
        });
    

    【讨论】:

    • 感谢您的信息,但仍然缺少 1 件事文件名是根据订单信息生成的变量,因此文件名不是静态使用您的代码我仍然需要检索文件名然后询问用户下载。
    • 现在我更了解您的问题,更新了更多部分的答案。
    • 非常感谢您的肯定,一件事是可以将用户留在应用程序内并从那里开始下载,而不是将他拉到网络浏览器来完成他的操作?
    • 是的。你可以这样做。见:stackoverflow.com/questions/15758856/…
    • 感谢分享。您的回答很有帮助,但是应用程序崩溃了,并且通过了我。
    猜你喜欢
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    相关资源
    最近更新 更多