J2SE實現網絡圖片的獲取
- public static void main(String[] args) throws Exception {
- String path = "http://img.linuxidc.com/img/Android.png";
-
- URL url = new URL(path);
-
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(5*1000);
- InputStream inStream = conn.getInputStream();
-
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
-
- int len=0;
- while((len=inStream.read(buffer))!=-1){
- outStream.write(buffer,0,len);
- }
-
- byte[] data = outStream.toByteArray();
-
- File f = new File("pic.jpg");
- FileOutputStream fos = new FileOutputStream(f);
- fos.write(data);
- fos.close();
- }
Androd中獲取網絡圖片
資源
- <string name="btn_text">顯示網絡圖片</string>
- <string name="error">下載圖片失敗!!</string>
布局
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/btn_text"
- android:id="@+id/showBtn"
- />
-
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/imageView"
- />
添加ImageService類
- package cn.class3g.service;
- …
- public class ImageService {
- public static byte[] getImage(String path) throws Exception{
- URL url = new URL(path);
- //get //post
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(5*1000);
- InputStream inStream = conn.getInputStream();
-
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int len = 0;
- while( (len = inStream.read(buffer)) !=-1 ){
- outStream.write(buffer, 0, len);
- }
- byte[] data = outStream.toByteArray();//圖片的二進制數據
- outStream.close();
- inStream.close();
- return data;
- }
- }
Activity
- public void onClick(View v) {
- String path = "http://img.linuxidc.com/img/Android.png";
-
- try {
- byte[] data = ImageService.getImage(path);
-
- Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
- imgView.setImageBitmap(bitmap);
-
- } catch (Exception e) {
- // e.printStackTrace();
- Log.e("TAG", e.toString());
- Toast.makeText(this, R.string.error, 1).show();
- }
- }
Androd中獲取網頁代碼
布局
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/btn_text"
- android:id="@+id/showBtn"
- />
-
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/imageView"
- />
代碼:在前面的ImageService.getImage()方法基礎上修改即可
HtmlService
- public class HtmlService {
- /**
- * 獲取給定路徑的html代碼
- * @param path 網頁路徑
- * @return
- * @throws Exception
- */
- public static String getHtml(String path) throws Exception{
- URL url = new URL(path);
- //get //post
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(5*1000);
- InputStream inStream = conn.getInputStream();
-
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int len = 0;
- while( (len = inStream.read(buffer)) !=-1 ){
- outStream.write(buffer, 0, len);
- }
- byte[] data = outStream.toByteArray();//網頁的二進制數據
- outStream.close();
- inStream.close();
- return new String(data, "gb2312");
- }
- }
ShowHtmlActivity
- public void onClick(View v) {
- String path = pathText.getText().toString();
- try {
- String htmlcode = HtmlService.getHtml(path);
- resultView.setText(htmlcode);
- } catch (Exception e) {
- Log.e(TAG, e.toString());
- Toast.makeText(ShowHtmlActivity.this, R.string.error, 1).show();
- }
- }