android下载文件到本地sdcard
import com.example.android.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity3 extends Activity {
private Button downfile = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
downfile = (Button) findViewById(R.id.downfile);
new Thread() {
@Override
public void run() {
// 你要执行的方法
downfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
HttpDownloader httpDownLoader = new HttpDownloader();
int result = httpDownLoader
.downfile(
"http://192.168.5.60:8080/statics/images//bdxz_btn.jpg",
"test/", "test.jpg");
// if (result == 0) {
// Toast.makeText(MainActivity3.this, "下载成功!",
// Toast.LENGTH_SHORT).show();
// } else if (result == 1) {
// Toast.makeText(MainActivity3.this, "已有文件!",
// Toast.LENGTH_SHORT).show();
// } else if (result == -1) {
// Toast.makeText(MainActivity3.this, "下载失败!",
// Toast.LENGTH_SHORT).show();
// }
}
});
// 执行完毕后给handler发送一个空消息
handler.sendEmptyMessage(0);
}
}.start();
}
// 定义Handler对象
private Handler handler = new Handler() {
@Override
// 当有消息发送出来的时候就执行Handler的这个方法
public void handleMessage(Message msg) {
super.handleMessage(msg);
// 处理UI
Log.e("MAINACTIVITY3", "" + msg);
}
};
}
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() {
// 得到当前外部存储设备的目录
// /SDCARD
SDPATH = Environment.getExternalStorageDirectory() + "/";
}
/**
* 在SD卡上创建文件
*
* @throws IOException
*/
public File createSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}
/**
* 在SD卡上创建目录
*
* @param dirName
*/
public File createSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
dir.mkdir();
return dir;
}
/**
* 判断SD卡上的文件夹是否存在
*/
public boolean isFileExist(String fileName) {
File file = new File(SDPATH + fileName);
return file.exists();
}
/**
* 将一个InputStream里面的数据写入到SD卡中
*/
public File write2SDFromInput(String path, String fileName,
InputStream input) {
File file = null;
OutputStream output = null;
try {
createSDDir(path);
file = createSDFile(path + fileName);
output = new FileOutputStream(file);
byte buffer[] = new byte[4 * 1024];
// while ((input.read(buffer)) != -1) {
// output.write(buffer);
// }
while (true) {
int temp = input.read(buffer, 0, buffer.length);
if (temp == -1) {
break;
}
output.write(buffer, 0, temp);
}
output.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return file;
}
}
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpDownloader {
private URL url = null;
FileUtils fileUtils = new FileUtils();
public int downfile(String urlStr, String path, String fileName) {
if (fileUtils.isFileExist(path + fileName)) {
return 1;
} else {
try {
InputStream input = null;
input = getInputStream(urlStr);
File resultFile = fileUtils.write2SDFromInput(path, fileName,
input);
if (resultFile == null) {
return -1;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 0;
}
// 由于得到一个InputStream对象是所有文件处理前必须的操作,所以将这个操作封装成了一个方法
public InputStream getInputStream(String urlStr) throws IOException {
InputStream is = null;
try {
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
urlConn.setRequestProperty("Connection", "close");
urlConn.connect();
is = urlConn.getInputStream();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return is;
}
}程序运行后提示:

请各位帮忙看下,谢谢!
...全文
相关推荐
安卓ADB上传链接
例如,要将本地的`test.txt`文件推送到设备的根目录下,可以执行以下命令:```shelladb push test.txt /sdcard/```要下载设备上/sdcard/目录下的`test.txt
在pc上的安卓平台的建立
从设备下载文件到电脑:`adb pull <远程路径> <本地路径>`,例如`adb pull sdcard/music.mp3 d:/music.mp3`。
Android开发实现加载网络图片并下载至本地SdCard的方法
Android开发实现加载网络图片并下载至本地SdCard的方法 Android开发实现加载网络图片并下载至本地SdCard的方法是指在 Android 应用程序中,通过网络加载图片,并将其下载到本地的
如何通过91手机助手安装Android系统应用软件.doc
点击左上角的"添加"按钮,可以手动选择电脑本地硬盘上的apk文件进行安装。通过"程序管理"界面,你还可以访问软件站,这是一个包含大量应用的资源库,可以直接下载并安装各种应用程序,无需通过其他途径。
adb 截图工具 --windows客户端使用
在Windows上,通常需要下载并安装Android SDK Platform Tools,其中包含了ADB工具。


