① android如何获取网络图片
android中获取网络图片是一件耗时的操作,如果直接获取有可能会出现应用程序无响应(ANR:Application Not Responding)对话框的情况。对于这种情况,一般的方法就是耗时操作用线程来实现。下面列三种获取url图片的方法:
直接获取:(容易:ANR,不建议)
java">mImageView=(ImageView)this.findViewById(R.id.imageThreadConcept);
Drawabledrawable=loadImageFromNetwork(IMAGE_URL);
mImageView.setImageDrawable(drawable);
2. 后台线程获取url图片:
mImageView=(ImageView)this.findViewById(R.id.imageThreadConcept);
newThread(newRunnable(){
Drawabledrawable=loadImageFromNetwork(IMAGE_URL);
@Override
publicvoidrun(){
//post()特别关键,就是到UI主线程去更新图片
mImageView.post(newRunnable(){
@Override
publicvoidrun(){
//TODOAuto-generatedmethodstub
mImageView.setImageDrawable(drawable);
}});
}
}).start();
3.AsyncTask获取url图片
mImageView=(ImageView)this.findViewById(R.id.imageThreadConcept);
newDownloadImageTask().execute(IMAGE_URL);
<String,Void,Drawable>
{
(String...urls){
returnloadImageFromNetwork(urls[0]);
}
protectedvoidonPostExecute(Drawableresult){
mImageView.setImageDrawable(result);
}
}
② android 网络图片查看器 无法获取服务器上的图片
你直接在手机的浏览器里面输入这个地址也可以显示出这个图片吗?
先怀疑一种可能性:你的手机上网方式不和电脑在一个局域网内部,也就是说不能通过192.168.***来访问你自己服务器上的图片,你可以先换一张公共网络图片的地址来调试。
③ 如何在Android当中显示网络图片
在android当中显示一张网络图片的时候,其实是比较麻烦的。首先得把这个网络图片转换成java的imputstream流,然后再把这个留转换成一个bitMap.
bitMap是可以作为参数传给imageView的。
在下边的returnBitMap函数是最核心的,也是大家可以重用的,它负责把一个url的网络图片变成一个本地的BitMap。
packagecom.jinyan.image;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.HttpURLConnection;
importjava.net.MalformedURLException;
importjava.net.URL;
importandroid.app.Activity;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.ImageView;
{
/**.*/
StringimageUrl="http://i.pbase.com/o6/92/229792/1/80199697.uAs58yHk.50pxCross_of_the_Knights_Templar_svg.png";
BitmapbmImg;
ImageViewimView;
Buttonbutton1;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imView=(ImageView)findViewById(R.id.imview);
imView.setImageBitmap(returnBitMap(imageUrl));
}
publicBitmapreturnBitMap(Stringurl){
URLmyFileUrl=null;
Bitmapbitmap=null;
try{
myFileUrl=newURL(url);
}catch(MalformedURLExceptione){
e.printStackTrace();
}
try{
HttpURLConnectionconn=(HttpURLConnection)myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStreamis=conn.getInputStream();
bitmap=BitmapFactory.decodeStream(is);
is.close();
}catch(IOExceptione){
e.printStackTrace();
}
returnbitmap;
}
}
④ 安卓开发 如何获取网络中的图片
用自带的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);
}
}
}
}
⑤ 想问一下在Android studio里面怎么实现显示网络图片,就是输入一个网址,显示一张图片
使用第三方图片加载框架如picasso,使用很简单,以下几步即可完成你的需求。
在app的build.gradle文件中添加依赖
implementation 'com.squareup.picasso:picasso:2.71828'
传入网络图片地址,以及要在哪个ImageView上显示
Picasso.get().load(imageurl).into(mImageView);
很简单,通过以上步骤,就可以完成在Android studio里面怎么实现显示网络图片,就是输入一个网址,显示一张图片。
⑥ Android 如何获取网络上的图片
android编程:
建议使用的框架:picasso
sharyuke
⑦ android根据url获取网络图片报错
这个看着是https协议的URL,用普通的http请求就报错了,我这里只有请求https到流的代码,给你先看看,把流再转成文件 就可以了
@SuppressLint("ParserError")
(StringdownUrl,StringpostStr)throwsIOException{
Stringres="";
HttpsURLConnection.setDefaultHostnameVerifier(newNullHostNameVerifier());
SSLContextcontext=null;
try{
context=SSLContext.getInstance("TLS");
}catch(NoSuchAlgorithmExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
try{
context.init(null,newX509TrustManager[]{newmyX509TrustManager()},newSecureRandom());
}catch(KeyManagementExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
URLdUrl=newURL(downUrl);
HttpsURLConnectiondConn=(HttpsURLConnection)dUrl.openConnection();
dConn.setDoInput(true);
if(postStr!=""){
dConn.setDoOutput(true);
dConn.setRequestMethod("POST");
}
dConn.connect();
if(postStr!=""){
try{
BufferedWriterout=newBufferedWriter(newOutputStreamWriter(
dConn.getOutputStream()));
out.write(postStr);
out.flush();
}catch(Exceptione){
StringerrMsg=e.getMessage();
if(null!=errMsg){
Toasttoast=Toast.makeText(null,errMsg,Toast.LENGTH_LONG);
toast.show();
}
e.printStackTrace();
}
}
BufferedInputStreamin=newBufferedInputStream(dConn.getInputStream());
returnin;
}
{
@Override
publicbooleanverify(Stringhostname,SSLSessionsession){
//Log.i("RestUtilImpl","Approvingcertificatefor"+hostname);
returntrue;
}
}
{
@Override
publicX509Certificate[]getAcceptedIssuers(){
returnnull;
}
@Override
publicvoidcheckClientTrusted(X509Certificate[]chain,StringauthType)
throwsCertificateException{
//TODOAuto-generatedmethodstub
}
@Override
publicvoidcheckServerTrusted(X509Certificate[]chain,StringauthType)
throwsCertificateException{
//TODOAuto-generatedmethodstub
}
}
⑧ android 网络获取图片从模糊到清晰
那个模糊的是先加载了一张分辨率小的预览图,然后当你点开那张预览图后,后台回去请求服务器,下载高分辨率的图片,完成后刷新imageView,就变清晰了。
⑨ 我用Android studio想显示网络图片却老是失败,甚至把网上的代码一字不动地照搬过来也是他们成功我失败,
请把代码提出来可以么? 网络图片显示失败,你看看你显示的网络图片url地址在浏览器上能不能打开,如果打不开,说明地址失效,当然就是失败的,如果不是,请把代码发出来看看。原因