一、权限的获取:
应用程序如要要使用联网功能,或者是操作SD卡,需要在AndroidManifest.xml中配置相应的权限:
二、要操作SD卡,首先要读取它在Android操作系统中的路径:
SDCardPath = Environment.getExternalStorageDirectory() + "/";
三、获取了SD卡的路径之后的操作,同于Java中的文件IO操作,此不赘述。
四、网络操作
1、由URL获取HTTP连接
import java.net.HttpURLConnection;
import java.net.URL;
url = new URL(strUrl); // 获取URL
// 获取HTTP连接
urlConn = (HttpURLConnection) url.openConnection();
2、由HTTP连接获取输入字节流
java.io.InputStream = urlConn.getInputStream()
3、其它操作同Java IO操作
五、示例代码:下载一个文本文件在控制行输出其内容,再下载这个文件保存早SD卡下
1、AndroidManifest.xml
package="me.bym"
android:versionCode="1"
android:versionName="1.0">
android:label="@string/app_name">
2、文件操作类FileHelper.java
package me.bym.utils;
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 FileHelper {
private String SDCardPath = null; // SD卡在Android文件系统中的路径
public String getPath() {
return SDCardPath;
}
/**
* 构造方法,获取SD卡在Android文件系统中的路径
*/
public FileHelper() {
SDCardPath = Environment.getExternalStorageDirectory() + "/";
}
/**
* 在SD卡上创建文件
*
* @param fileName
* 要创建的文件名
* @return 创建得到的文件
*/
public File createSDFile(String fileName) throws IOException {
File file = new File(SDCardPath + fileName);
file.createNewFile();
return file;
}
/**
* 在SD卡上创建目录
*
* @param dirName
* 要创建的目录名
* @return 创建得到的目录
*/
public File createSDDir(String dirName) {
File dir = new File(SDCardPath + dirName);
dir.mkdir();
return dir;
}
/**
* 判断文件是否已经存在
*
* @param fileName
* 要检查的文件名
* @return boolean, true表示存在,false表示不存在
*/
public boolean isFileExist(String fileName) {
File file = new File(SDCardPath + fileName);
return file.exists();
}
/**
* 将一个输入流中的内容写入到SD卡上生成文件
*
* @param path
* 文件目录
* @param fileName
* 文件名
* @param inputStream
* 字节输入流
* @return 得到的文件
*/
public File writeToSDCard(String path, String fileName,
InputStream inputStream) {
File file = null;
OutputStream output = null;
try{
createSDDir(path);
file = createSDFile(path + fileName);
output = new FileOutputStream(file);
byte buffer [] = new byte[4 * 1024];
while((inputStream.read(buffer)) != -1){
output.write(buffer);
}
output.flush();
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
output.close();
}
catch(Exception e){
e.printStackTrace();
}
}
return file;
}
}
3、HTTP下载操作类HttpDownloadHelper.java
package me.bym.utils;
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.URL;
public class HttpDownloadHelper {
private URL url = null;
private HttpURLConnection urlConn = null;
private final int SUCCESS = 1;
private final int EXIST = 0;
private final int ERROR = -1;
/**
* 通过传递一个url下载对应的文本文件。
*
* @param strUrl
* 下载文件的URL地址
* @return 下载得到的文本文件内容字符串
*/
public String textDownload(String strUrl) {
StringBuilder sb = new StringBuilder(512); // 初始化文本内容缓冲区。
String line = null;
BufferedReader br = null;
try {
url = new URL(strUrl); // 获取URL
// 获取HTTP连接
urlConn = (HttpURLConnection) url.openConnection();
br = new BufferedReader(new InputStreamReader(
urlConn.getInputStream()));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
/**
* 通过传递一个url下载对应的文件。
*
* @param strUrl
* 下载文件的url地址
* @param path
* 保存文件的路径
* @param fileName
* 保存文件的文件名
* @return SUCCESS:文件下载成功 ERROR:文件下载失败 EXIST:同名文件已存在
*/
public int fileDownload(String strUrl, String path, String fileName) {
FileHelper fileHelper = new FileHelper();
InputStream inputStream = null;
File file = null;
if (fileHelper.isFileExist(path + fileName)) {
return EXIST;
} else {
try {
inputStream = getInputStream(strUrl);
file = fileHelper.writeToSDCard(path, fileName, inputStream);
if (file == null) {
return ERROR;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return SUCCESS;
}
/**
* 通过一个url获取http连接的输入流
*
* @param strUrl
* 目标url
* @return 到该url的http连接的输入流
* @throws IOException
*/
private InputStream getInputStream(String strUrl) throws IOException {
url = new URL(strUrl);
urlConn = (HttpURLConnection) url.openConnection();
InputStream is = urlConn.getInputStream();
return is;
}
}
4、Activity类
package me.bym;
import me.bym.utils.HttpDownloadHelper;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class DownLoadActivity extends Activity {
private Button downloadText = null;
private Button downloadFile = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
downloadText = (Button) findViewById(R.id.downloadText);
downloadText.setOnClickListener(new downTextListener());
downloadFile = (Button) findViewById(R.id.downloadFile);
downloadFile.setOnClickListener(new downFileListener());
}
class downTextListener implements OnClickListener {
@Override
public void onClick(View v) {
HttpDownloadHelper downHelper = new HttpDownloadHelper();
String str = downHelper
.textDownload("http://news.swjtu.edu.cn/shownews-2638.html");
System.out.println(str);
}
}
class downFileListener implements OnClickListener {
@Override
public void onClick(View v) {
HttpDownloadHelper downHelper = new HttpDownloadHelper();
int res = downHelper.fileDownload(
"http://www.swjtu.edu.cn/images/swjtu_title.jpg", "myImg/", "a.html");
switch (res) {
case 0:
System.out.println("文件已存在");
break;
case 1:
System.out.println("文件下载成功");
break;
case -1:
System.out.println("文件下载失败");
break;
}
}
}
}
http://blog.csdn.net/baoyiming1991/article/details/6307373