以下是我使用過的android中下載檔案到SD卡的方法,挺好使的。在這裡我只把下載的代碼給貼了出來,你在哪裡調用這個方法,直接使用就可以了。當然,具體問題要具體對待,這段代碼僅供參考,你可以修改,完全可以變成自己覺得方便使用的代碼:
1.HttpDownloader類
package com.pp.tool;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpDownloader {
private URL url = null;
/**
* 根據URL下載檔案,前提是這個檔案當中的內容是文本,函數的傳回值就是檔案當中的內容
* 1.建立一個URL對象
* 2.通過URL對象,建立一個HttpURLConnection對象
* 3.得到InputStram
* 4.從InputStream當中讀取資料
* @param urlStr
* @return
*/
public String download(String urlStr) {
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try {
// 建立一個URL對象
url = new URL(urlStr);
// 建立一個Http串連
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 使用IO流讀取資料
buffer = new BufferedReader(new InputStreamReader(urlConn
.getInputStream(),"UTF-8"));
while ((line = buffer.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buffer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return sb.toString();
}
/**
* 該函數返回整形 -1:代表下載檔案出錯 0:代表下載檔案成功 1:代表檔案已經存在
*/
public int downFile(String urlStr, String path, String fileName) {
InputStream inputStream = null;
try {
FileUtils fileUtils = new FileUtils();
if (fileUtils.isFileExist(path + fileName)) {
return 1;
} else {
inputStream = getInputStreamFromUrl(urlStr);
File resultFile = fileUtils.writeToSDFromInput(path,fileName, inputStream);
if (resultFile == null) {
return -1;
}
}
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
try {
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return 0;
}
/**
* 根據URL得到輸入資料流
*
* @param urlStr
* @return
* @throws MalformedURLException
* @throws IOException
*/
public InputStream getInputStreamFromUrl(String urlStr)
throws MalformedURLException, IOException {
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConn.getInputStream();
return inputStream;
}
}
2.FileUtils類
package com.pp.tool;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.os.Environment;
public class FileUtils {
private String SDPATH;
public String getSDPATH() {
return SDPATH;
}
public FileUtils() {
//得到當前外部存放裝置的目錄
SDPATH = Environment.getExternalStorageDirectory() + "/";
}
/**
* 在SD卡上建立檔案
*
* @throws IOException
*/
public File creatSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}
/**
* 在SD卡上建立目錄
*
* @param dirName
*/
public File creatSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
dir.mkdir();
return dir;
}
/**
* 判斷SD卡上的檔案夾是否存在
*/
public boolean isFileExist(String fileName){
File file = new File(SDPATH + fileName);
file.delete();
return file.exists();
}
/**
* 將一個InputStream裡面的資料寫入到SD卡中
*/
public File writeToSDFromInput(String path,String fileName,InputStream input){
File file =null;
OutputStream output =null;
try{
creatSDDir(path);
file = creatSDFile(path + fileName);
output = new FileOutputStream(file);
byte buffer [] = new byte[1024];
int len = 0;
//如果下載成功就開往SD卡裡些資料
while((len =input.read(buffer)) != -1){
output.write(buffer,0,len);
}
output.flush();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
input.close();
output.close();
}catch(Exception e){
e.printStackTrace();
}
}
return file;
}
}