Android讀取服務器圖片的三種方法
Android鏈接服務器獲取圖片在此提供三種方法,已通過驗證,無誤。
方法一:
public static Bitmap getImage(String path){
try {
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
System.out.println("tdw1");
if(conn.getResponseCode() == 200){
InputStream inputStream = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
在第一種方法中,從conn的輸入流中獲取數(shù)據(jù)將其轉化為Bitmap型數(shù)據(jù)。
在功能代碼中:
image.setImageBitmap(getImage("路徑"));
image為ImageView型控件。
第二種方法:
public static Bitmap getImage1(String path){
HttpGet get = new HttpGet(path);
HttpClient client = new DefaultHttpClient();
Bitmap pic = null;
try {
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
pic = BitmapFactory.decodeStream(is); // 關鍵是這句代
} catch (Exception e) {
e.printStackTrace();
}
return pic;
}
這個方法類似上面那個方法。在功能代碼中設置是一樣的
第三種方法:
public static Uri getImage2(String path,File cacheDir){
File localFile = new File(cacheDir,MD5.getMD5(path)+path.substring(path.lastIndexOf(".")));
if(localFile.exists()){
return Uri.fromFile(localFile);
}else
{
HttpURLConnection conn;
try {
conn = (HttpURLConnection) new URL(path).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() == 200){
System.out.println("tdw");
FileOutputStream outputStream = new FileOutputStream(localFile);
InputStream inputStream = conn.getInputStream();
byte[] buffer = new byte[1024];
int length = 0;
while((length=inputStream.read(buffer))!=-1){
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
return Uri.fromFile(localFile);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
第三種方法,將從服務器獲取的數(shù)據(jù)存入本地的文件中,如果文件已存在,則不需要從服務器重新獲取數(shù)據(jù)。
在功能代碼中:
image.setImageURI(getImage2(path, cache));
上面代碼中設置圖片為緩存設置,這樣如果圖片資源更新了,則需要重新命名文件的名字,這樣才能夠重新加載新圖片。
cache = new File(Environment.getExternalStorageDirectory(),"cache");
if(!cache.exists()){
cache.mkdirs();
}
這里是設置 緩存圖片的路徑。
以上為三種方法。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android將圖片上傳到php服務器的實例代碼
- Android 通過Base64上傳圖片到服務器實現(xiàn)實例
- Android 通過webservice上傳多張圖片到指定服務器詳解
- Android選擇圖片或拍照圖片上傳到服務器
- Android開發(fā)中調用系統(tǒng)相冊上傳圖片到服務器OPPO等部分手機上出現(xiàn)短暫的顯示桌面問題的解決方法
- Android Socket服務端與客戶端用字符串的方式互相傳遞圖片的方法
- Android使用post方式上傳圖片到服務器的方法
- Android異步上傳圖片到PHP服務器
- Android從服務器獲取圖片的實例方法
- android傳送照片到FTP服務器的實現(xiàn)代碼
相關文章
安裝android開發(fā)環(huán)境原始版(windows版)
安裝android開發(fā)環(huán)境原始版(windows版)的詳細步驟2013-03-03
Android監(jiān)聽手機電話狀態(tài)與發(fā)送郵件通知來電號碼的方法(基于PhoneStateListene實現(xiàn))
這篇文章主要介紹了Android監(jiān)聽手機電話狀態(tài)與發(fā)送郵件通知來電號碼的方法,通過Android的PhoneStateListene實現(xiàn)該功能,需要的朋友可以參考下2016-01-01
Android GridView中包含EditText的焦點重新獲取方法
這篇文章主要介紹了Android GridView中包含EditText的焦點重新獲取方法,實例分析了界面刷新時EditText重新獲取焦點的技巧,需要的朋友可以參考下2016-03-03
Android筆記之:在ScrollView中嵌套ListView的方法
本篇文章是對Android中在ScrollView中嵌套ListView的方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05
Flutter runApp GestureBinding使用介紹
這篇文章主要為大家介紹了Flutter runApp GestureBinding使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
Android編程實現(xiàn)加載等待ProgressDialog的方法
這篇文章主要介紹了Android編程實現(xiàn)加載等待ProgressDialog的方法,實例分析了Android中加載等待類ProgressDialog的具體使用方法,需要的朋友可以參考下2015-12-12
Android系統(tǒng)進程間通信Binder機制在應用程序框架層的Java接口源代碼分析
本文主要介紹 Android系統(tǒng)進程間通信Binder機制Java 接口源碼分析,這里詳細介紹了如何實現(xiàn)Binder 機制和Java接口直接的通信,有興趣的小伙伴可以參考下2016-08-08

