導航:首頁 > 操作系統 > android查看網路圖片

android查看網路圖片

發布時間:2022-08-11 01:50:20

android如何獲取網路圖片

android中獲取網路圖片是一件耗時的操作,如果直接獲取有可能會出現應用程序無響應(ANR:Application Not Responding)對話框的情況。對於這種情況,一般的方法就是耗時操作用線程來實現。下面列三種獲取url圖片的方法:


  1. 直接獲取:(容易: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,使用很簡單,以下幾步即可完成你的需求。

  1. 在app的build.gradle文件中添加依賴

    implementation 'com.squareup.picasso:picasso:2.71828'

  2. 傳入網路圖片地址,以及要在哪個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地址在瀏覽器上能不能打開,如果打不開,說明地址失效,當然就是失敗的,如果不是,請把代碼發出來看看。原因

閱讀全文

與android查看網路圖片相關的資料

熱點內容
androidapi中文合集 瀏覽:658
win7下安裝linux虛擬機 瀏覽:838
雲主機用別的伺服器嗎 瀏覽:922
黑馬買入指標源碼副圖 瀏覽:962
微軟為什麼會把伺服器放在水底 瀏覽:257
php截取字元串中文 瀏覽:21
虛擬機和編譯軟體哪個好 瀏覽:750
存儲伺服器為什麼比通用伺服器難 瀏覽:373
用php列印出前一天的時間 瀏覽:369
2010編譯方法 瀏覽:239
華為哪裡查看隱藏app 瀏覽:889
linux網卡重置 瀏覽:830
框架柱低於四米箍筋全高加密 瀏覽:694
米二如何安卓版本升級到高安卓版 瀏覽:783
安卓手機數據慢怎麼辦 瀏覽:727
雙底買賣指標公式源碼無未來函數 瀏覽:685
我的世界伺服器換電腦怎麼玩 瀏覽:215
linux內核源碼內存調優 瀏覽:789
活塞壓縮機廠家 瀏覽:492
java程序員macos 瀏覽:982