Android 通過網(wǎng)絡(luò)圖片路徑查看圖片實(shí)例詳解
Android 通過網(wǎng)絡(luò)圖片路徑查看圖片實(shí)例詳解
1.在項(xiàng)目清單中添加網(wǎng)絡(luò)訪問權(quán)限
<!--訪問網(wǎng)絡(luò)的權(quán)限--> <uses-permission android:name="android.permission.INTERNET"/>
2.獲取網(wǎng)絡(luò)圖片數(shù)據(jù)
/**
* 獲取網(wǎng)絡(luò)圖片的數(shù)據(jù)
* @param path 網(wǎng)絡(luò)圖片路徑
* @return
* @throws Exception
*/
public static byte[] getImage(String path) throws Exception {
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();//得到基于HTTP協(xié)議的連接對象
conn.setConnectTimeout(5000);//設(shè)置超時(shí)時(shí)間
conn.setRequestMethod("GET");//請求方式
if(conn.getResponseCode()==200){//判斷是否請求成功
InputStream inputStream=conn.getInputStream();
return read(inputStream);
}
return null;
}
/**
* 讀取流中的數(shù)據(jù)
*/
public static byte[] read(InputStream inputStream) throws IOException {
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
byte[] b=new byte[1024];
int len=0;
while((len=inputStream.read(b))!=-1){
outputStream.write(b);
}
inputStream.close();
return outputStream.toByteArray();
}
3.處理查看圖片的控制
public class NetimageActivity extends Activity {
private EditText pathText;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pathText=(EditText)this.findViewById(R.id.imagepath);//圖片路徑
imageView=(ImageView)this.findViewById(R.id.imageView);//顯示圖片控件
Button button=(Button)this.findViewById(R.id.button);//查看圖片按鈕
button.setOnClickListener(new ButtonClickListener());//注冊查看圖片按鈕事件
}
/**
* 處理查看圖片按鈕事件
*/
private final class ButtonClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
//取得圖片路徑
String path=pathText.getText().toString();
try {
//獲取圖片數(shù)據(jù)
byte[] data=ImageService.getImage(path);
//使用數(shù)組的所有數(shù)據(jù)構(gòu)建位圖對象
Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bitmap);//顯示圖片
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), R.string.error, 1).show();
}
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- android查看網(wǎng)絡(luò)圖片的實(shí)現(xiàn)方法
- Android圖片處理教程之全景查看效果實(shí)現(xiàn)
- Android仿百度圖片查看功能
- Android 簡單的圖片查看器源碼實(shí)現(xiàn)
- android自定義Camera拍照并查看圖片
- Android 網(wǎng)絡(luò)圖片查看器與網(wǎng)頁源碼查看器
- android網(wǎng)絡(luò)圖片查看器簡單實(shí)現(xiàn)代碼
- Android 實(shí)現(xiàn)WebView點(diǎn)擊圖片查看大圖列表及圖片保存功能
- Android實(shí)現(xiàn)圖片查看功能
相關(guān)文章
ListView實(shí)現(xiàn)聊天列表之處理不同數(shù)據(jù)項(xiàng)
這篇文章主要為大家詳細(xì)介紹了ListView實(shí)現(xiàn)聊天列表之處理不同數(shù)據(jù)項(xiàng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android 深入探究自定義view之流式布局FlowLayout的使用
FlowLayout(int align, int hgap, int vgap)創(chuàng)建一個新的流布局管理器,它具有指定的對齊方式以及指定的水平和垂直間隙,意思就是說從左上角開始添加原件,依次往后排,第一行擠滿了就換一行接著排2021-11-11
Android提高之SurfaceView的基本用法實(shí)例分析
這篇文章主要介紹了Android提高之SurfaceView的基本用法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08
Android AsyncTask 后監(jiān)聽異步加載完畢的動作詳解
這篇文章主要介紹了Android 使用AsyncTask 后監(jiān)聽異步加載完畢的動作的相關(guān)資料,需要的朋友可以參考下2016-11-11
Flutter倒計(jì)時(shí)/計(jì)時(shí)器的實(shí)現(xiàn)代碼
這篇文章主要介紹了Flutter倒計(jì)時(shí)/計(jì)時(shí)器的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Android自定義View實(shí)現(xiàn)天氣預(yù)報(bào)折線圖
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)天氣預(yù)報(bào)折線圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Android仿Iphone屏幕底部彈出半透明PopupWindow效果
這篇文章主要為大家詳細(xì)介紹了Android仿Iphone屏幕底部彈出半透明PopupWindow效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android 根據(jù)EditText搜索框ListView動態(tài)顯示數(shù)據(jù)
這篇文章主要介紹了Android 根據(jù)EditText搜索框ListView動態(tài)顯示數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2016-09-09

