WiTon Nope
WiTon Nope

Reputation: 166

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. Now I am trying to work on a code for the App to read the link from the thankyou.php page

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

The file is a ".DAT" extension and coming from a certain link. The App should retrieve the link in a new page and let the customer download the file.

enter image description here

Upvotes: 0

Views: 4874

Answers (1)

Cory Roy
Cory Roy

Reputation: 5609

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:

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.

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);
        }
    });

Upvotes: 4

Related Questions