在Android2.3中,官網已經提供透過http下載檔案的API,請看官網說明。
這裡直接使用範例
package com.test.testdownload;
import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.net.Uri;
import android.os.Bundle;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
//指定檔案來源,必需是http,hppts會有錯誤
Request request = new Request(
Uri.parse("http://www.ballpure.com/uploads4/userup/1107/25003614Z55.jpg"));
//存檔路徑及檔名
Uri uri = Uri.parse("file:///mnt/sdcard/download_name.jpg");
request.setDestinationUri(uri);
//開始下載
dm.enqueue(request);
}
}
AndroidManifest.xml 如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.testdownload"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
這裡直接使用範例
package com.test.testdownload;
import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.net.Uri;
import android.os.Bundle;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
//指定檔案來源,必需是http,hppts會有錯誤
Request request = new Request(
Uri.parse("http://www.ballpure.com/uploads4/userup/1107/25003614Z55.jpg"));
//存檔路徑及檔名
Uri uri = Uri.parse("file:///mnt/sdcard/download_name.jpg");
request.setDestinationUri(uri);
//開始下載
dm.enqueue(request);
}
}
AndroidManifest.xml 如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.testdownload"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
沒有留言:
張貼留言