国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 通過網(wǎng)絡圖片路徑查看圖片實例詳解

2019-12-12 02:45:28
字體:
來源:轉載
供稿:網(wǎng)友

Android 通過網(wǎng)絡圖片路徑查看圖片實例詳解

1.在項目清單中添加網(wǎng)絡訪問權限

<!--訪問網(wǎng)絡的權限--> <uses-permission android:name="android.permission.INTERNET"/> 

2.獲取網(wǎng)絡圖片數(shù)據(jù)

/**    * 獲取網(wǎng)絡圖片的數(shù)據(jù)    * @param path 網(wǎng)絡圖片路徑    * @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);//設置超時時間     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ù)構建位圖對象         Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);         imageView.setImageBitmap(bitmap);//顯示圖片       } catch (Exception e) {         e.printStackTrace();         Toast.makeText(getApplicationContext(), R.string.error, 1).show();       }     }   } } 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 巴青县| 禹州市| 延庆县| 永康市| 灯塔市| 桃园县| 临武县| 民和| 临沧市| 株洲县| 安岳县| 博野县| 峨山| 临潭县| 巴楚县| 江安县| 定日县| 荣成市| 舒兰市| 定边县| 安丘市| 神木县| 朝阳区| 广东省| 天津市| 东平县| 凤庆县| 甘德县| 梁河县| 淮安市| 滨海县| 阜南县| 黎川县| 象山县| 承德市| 商城县| 洛南县| 南澳县| 亳州市| 太原市| 乡宁县|