专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Android

android文件上传时,界面需有提示,在线解决方法

发布时间:2010-05-30 10:50:09 文章来源:www.iduyao.cn 采编人员:星星草
android文件上传时,界面需有提示,在线
android程序中,下面是我上传文件到服务器中的代码,代码是可成功运行的,如下:

private void uploadFile(String path)//path格式 /mnt/sdcard/DCIM/Camera/2011-12-16-100120.jpg 
{
String end = "rn";
String twoHyphens = "--";
String boundary = "*****";
try
{
URL url =new URL(actionUrl);
HttpURLConnection con=(HttpURLConnection)url.openConnection();  
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设定传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary="+boundary);
/* 设定DataOutputStream */
DataOutputStream ds = 
new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; " +
"name="uploadfile";filename="" +
newName +""" + end);
ds.writeBytes(end);  

/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(path);
/* 设定每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];

int length = -1;
/* 从文件读取数据到缓冲区 */
while((length = fStream.read(buffer)) != -1)
{
/* 将数据写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);

/* close streams */
fStream.close();
ds.flush();
/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 )
{
b.append( (char)ch );
}
/* 将Response显示于Dialog */
showDialog(b.toString().trim());
/* 关闭DataOutputStream */
ds.close();
}
catch(Exception e)
{
showDialog(""+e);
}
}
现问题是,在文件上传时,手机界面会一直黑着,并不动,直接上传成功,界面才变,现需:在上传时,手机界面要有滚动条或其它方式的提示,不能一直黑在那,怎么实现呀,thanks

------解决方案--------------------
显示进度条:
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setTitle(getResources().getString(R.string.reading));
progressDialog.setMessage(getResources().getString(R.string.lateron));
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(false);
progressDialog.show();

隐藏进度条:
progressDialog.dismiss()
------解决方案--------------------
其实可以直接起一个进度条,提示一下!
------解决方案--------------------
这个需要用线程来处理的
------解决方案--------------------
楼主 在线程里面使用handle,在线程里面操作上传。
------解决方案--------------------
受教了,多谢
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: