安卓开发 如何获取网络中的图片
1个回答
展开全部
用自带的HttpClient,下面是我自己的库里面取出来的一个方法,我是异步ajax调用的,你要是自己用的话,把那些异步回调的去掉就行了。
url传入图片地址,outputFile是输出的文件对象,也就是说,要你先指定保存的文件位置。
只要没限制外链的图片文件应该都没问题。
/**
* 下载文件
* @param url
* @param outPut
* @param listener
* @throws AjaxException
*/
public static Responses downLoad(String url, AjaxParameters params,File outputFile, TransmitProgressLitener uploadListener) throws AjaxException{
if(params.size()>0){
url = url + "?" + Utils.encodeUrl(params);
}
Utils.amLog(url);
//httpGet连接对象
HttpGet httpRequest = new HttpGet(url);
//取得HttpClient 对象
//HttpClient httpclient = new DefaultHttpClient();
HttpClient httpclient = getNewHttpClient();
InputStream is = null;
OutputStream os= null;
try {
//请求httpClient ,取得HttpRestponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
//取得相关信息 取得HttpEntiy
HttpEntity httpEntity = httpResponse.getEntity();
//获得一个输入流
is = httpEntity.getContent();
os = new FileOutputStream(outputFile,true);
long downloaded = 0;
byte[] buffer=new byte[IO_CACHE_SIZE];
while(true){
int count=is.read(buffer);
if(count==-1){
break;
}
os.write(buffer, 0, count);
if(null!=uploadListener){
downloaded +=count;
uploadListener.updateProgress(downloaded, 0);
}
}
}
return new Responses("download suc",((DefaultHttpClient)httpclient).getCookieStore(),((DefaultHttpClient)httpclient).getCookieSpecs());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new AjaxException(e);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new AjaxException(e);
} finally{
if(null!=is){
try {
is.close();
} catch (IOException e) {
throw new AjaxException(e);
}
}
if(null!=os){
try {
os.close();
} catch (IOException e) {
throw new AjaxException(e);
}
}
}
}
url传入图片地址,outputFile是输出的文件对象,也就是说,要你先指定保存的文件位置。
只要没限制外链的图片文件应该都没问题。
/**
* 下载文件
* @param url
* @param outPut
* @param listener
* @throws AjaxException
*/
public static Responses downLoad(String url, AjaxParameters params,File outputFile, TransmitProgressLitener uploadListener) throws AjaxException{
if(params.size()>0){
url = url + "?" + Utils.encodeUrl(params);
}
Utils.amLog(url);
//httpGet连接对象
HttpGet httpRequest = new HttpGet(url);
//取得HttpClient 对象
//HttpClient httpclient = new DefaultHttpClient();
HttpClient httpclient = getNewHttpClient();
InputStream is = null;
OutputStream os= null;
try {
//请求httpClient ,取得HttpRestponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
//取得相关信息 取得HttpEntiy
HttpEntity httpEntity = httpResponse.getEntity();
//获得一个输入流
is = httpEntity.getContent();
os = new FileOutputStream(outputFile,true);
long downloaded = 0;
byte[] buffer=new byte[IO_CACHE_SIZE];
while(true){
int count=is.read(buffer);
if(count==-1){
break;
}
os.write(buffer, 0, count);
if(null!=uploadListener){
downloaded +=count;
uploadListener.updateProgress(downloaded, 0);
}
}
}
return new Responses("download suc",((DefaultHttpClient)httpclient).getCookieStore(),((DefaultHttpClient)httpclient).getCookieSpecs());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new AjaxException(e);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new AjaxException(e);
} finally{
if(null!=is){
try {
is.close();
} catch (IOException e) {
throw new AjaxException(e);
}
}
if(null!=os){
try {
os.close();
} catch (IOException e) {
throw new AjaxException(e);
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询