该篇记录的程序,使用的网络上的一段代码,只是在使用过程中出现问题,所以记录下来,方便以后使用,原程序地址如下:
http://greatverve.cnblogs.com/archive/2011/12/23/android-upload.html
问题:执行到 conn.getOutputStream() 时,抛出IO错误。
解决:Android 从4.0 开始,对于网络数据的操作,必须在子线程中完成,所以新建个线程执行文件上传就可以了。
protected class PostThread implements Runnable {
@Override
public void run() {
File file = new File(picPath);
if (file != null) {
String request = UploadUtil.uploadFile(file, requestURL);
}
}
}