① 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地址在瀏覽器上能不能打開,如果打不開,說明地址失效,當然就是失敗的,如果不是,請把代碼發出來看看。原因