Android中图片的上传和下载代码
发布时间:2020-05-24 15:28:55 所属栏目:Java 来源:互联网
导读:Android中图片的上传和下载代码
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 客户端代码:File file = new File(imageUrl);
String httpUrl = httpDomain+"AddImageServlet"+"?gid="+gid;
HttpPost request = new HttpPost(httpUrl);
HttpClient httpClient = new DefaultHttpClient();
FileEntity entity = new FileEntity(file,"binary/octet-stream");
HttpResponse response;
try {
request.setEntity(entity);
entity.setContentEncoding("binary/octet-stream");
response = httpClient.execute(request);
//如果返回状态为200,获得返回的结果
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
……//图片上传成功
}
}
catch(Exception e){
}
//获得新闻id
String gid = request.getParameter("gid");
String filePath = getRealPath(request) + "userpic";
// 定义上载文件的最大字节
int MAX_SIZE = 102400 * 102400;
// 声明文件读入类
DataInputStream in = null;
FileOutputStream fileOut = null;
// 取得客户端上传的数据类型
String contentType = request.getContentType();
if(contentType.indexOf("binary/octet-stream") >= 0){
// 读入上传的数据
in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
// 如果图片过大
if(formDataLength > MAX_SIZE){
String errormsg=("上传的文件字节数不可以超过" + MAX_SIZE);
out.println(errormsg);
return ;
}
// 保存上传文件的数据
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
// 上传的数据保存在byte数组
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes,totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String fileName = filePath + gid+".png";
// 检查上载文件的目录是否存在
File fileDir = new File(filePath);
if(!fileDir.exists()){
fileDir.mkdirs();
}
// 创建文件的写出类
fileOut = new FileOutputStream(fileName);
// 保存文件的数据
fileOut.write(dataBytes);
fileOut.close();
//保存文件的路径名
……
2 图片下载:首先获得网络图片的图片地址,发送请求后,服务器将会返回该图片的字节流,利用BitmapFactory.decodeStream()方法将字节流转化为图片并返回。具体代码如下: //获得网络中的图片
public Bitmap getGossipImage(String gid){
String httpUrl = httpDomain+"userpic/"+gid+".png";
Bitmap bitmap = null;
HttpGet httpRequest = new HttpGet(httpUrl);
//取得HttpClient 对象
HttpClient httpclient = new DefaultHttpClient();
try {
//请求httpClient ,取得HttpRestponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
//取得相关信息 取得HttpEntiy
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
bitmap = BitmapFactory.decodeStream(is);
is.close();
}else{
Toast.makeText(context,"连接失败!",Toast.LENGTH_SHORT).show();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
