简体   繁体   English

Android从链接下载文件

[英]Android Download file from a link

After I've implemented a solution for the Android app to post to web server and validate Google Order then issue a download link. 在为Android应用程序实施解决方案后,它可以发布到Web服务器并验证Google Order,然后发布下载链接。 Now I am trying to work on a code for the App to read the link from the thankyou.php page 现在,我正在尝试为该应用编写代码,以从thankyou.php页面读取链接

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

The file is a ".DAT" extension and coming from a certain link. 该文件是一个“ .DAT”扩展名,来自某个链接。 The App should retrieve the link in a new page and let the customer download the file. 该应用程序应在新页面中检索链接,并让客户下载文件。

在此处输入图片说明

Inside you manifest file you need to add an intent filter: 在清单文件内部,您需要添加一个意图过滤器:

    <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>

Then inside your "Download" activity's onCreate: 然后在“下载”活动的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);
    }
}

Then in the clickhandler for the download button you can use a view intent to act like a link in android. 然后在用于下载按钮的clickhandler中,您可以使用视图意图像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);
        }
    });

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

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