您的位置:首页 > 理论基础 > 计算机网络

Android之网络操作 - 从网络获取图片或网页

2011-09-26 22:12 429 查看
1.在Java中操作显示网络图片

view
plain

public class ImageRequest

{

/**

* @param args

*/

public static void main(String[] args) throws Exception

{

//构造一个URL对象

URL url = new URL("http://www.baidu.com/img/baidu_sylogo1.gif");

//使用openConnection打开URL对象

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

//使用Http协议,设置请求方式为GET

conn.setRequestMethod("GET");

//设置链接超时异常,5s

conn.setConnectTimeout(5 * 1000);

//通过输入流获取图片数据

InputStream inStream = conn.getInputStream();

//获取到图片的二进制数据

byte[] data = readInputStream(inStream);

//构造一个文件,保存图片到项目的根目录下

File imageFile = new File("baidu_logo.jpg");

//构造一个文件输出流FileOutputStream

FileOutputStream outStream = new FileOutputStream(imageFile);

//把文件数据写入到输出流

outStream.write(data);

outStream.close();

}



/**

* 从输入流里面得到返回为二进制的数据

* @param inStream 输入流

* @return byte[] 二进制数据

* @throws Exception

*/

public static byte[] readInputStream(InputStream inStream) throws Exception

{

//构造一个ByteArrayOutputStream

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

//设置一个缓冲区

byte[] buffer = new byte[1024];

int len = 0;

//判断输入流长度是否等于-1,即非空

while( (len=inStream.read(buffer)) != -1 )

{

//把缓冲区的内容写入到输出流中,从0开始读取,长度为len

outStream.write(buffer, 0, len);

}

//关闭输入流

inStream.close();

return outStream.toByteArray();

}

}





2.Android中显示图片:

(1)ImageShowActivity.java

view
plain

public class ImageShowActivity extends Activity

{

private static final String TAG = "ImageShowActivity";

private EditText pathText;

private ImageView imageView;



@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//得到EditText,ImageView与Button

pathText = (EditText) this.findViewById(R.id.urlpath);

imageView = (ImageView) this.findViewById(R.id.imageView);

Button button = (Button)this.findViewById(R.id.button);

//设置Button监听

button.setOnClickListener(new View.OnClickListener()

{

@Override

public void onClick(View v)

{

//获取EditText里面的地址

String path = pathText.getText().toString();

try

{

//得到图片的二进制数据

byte[] data = ImageService.getImage(path);

//图片处理

Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);//生成位图

//显示图片

imageView.setImageBitmap(bitmap);

}

catch (Exception e)

{

//出错的时候提示错误信息

Toast.makeText(ImageShowActivity.this, R.string.error, 1).show();

//Log里面打印错误信息

Log.e(TAG, e.toString());

}

}

});

}

}



(2)编写一个流处理工具类,StreamTool.java





view
plain

public class StreamTool

{

/**

* 从输入流里面得到返回为二进制的数据

* @param inStream 输入流

* @return byte[] 二进制数据

* @throws Exception

*/

public static byte[] readInputStream(InputStream inStream) throws Exception

{

//构造一个ByteArrayOutputStream

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

//设置一个缓冲区

byte[] buffer = new byte[1024];

int len = 0;

//判断输入流长度是否等于-1,即非空

while( (len=inStream.read(buffer)) != -1 )

{

//把缓冲区的内容写入到输出流中,从0开始读取,长度为len

outStream.write(buffer, 0, len);

}

//关闭输入流

inStream.close();

return outStream.toByteArray();

}

}



(3)编写一个图片处理类,打开URL,获取输入流等操作





view
plain

public class ImageService

{

public static byte[] getImage(String path) throws Exception

{

//构造一个URL对象

URL url = new URL("http://www.baidu.com/img/baidu_sylogo1.gif");

//使用openConnection打开URL对象

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

//使用Http协议,设置请求方式为GET

conn.setRequestMethod("GET");

//设置链接超时异常,5s

conn.setConnectTimeout(5 * 1000);

//通过输入流获取图片数据

InputStream inStream = conn.getInputStream();

//返回图片的二进制数据

return StreamTool.readInputStream(inStream);

}

}



注意别忘记在AndroidManifest.xml文件中添加访问网络的权限:

<uses-permission android:name="android.permission.INTERNET"/>



3.在Android中显示网页代码:通过滚动条视图(ScrollView)工具显示代码

(1)main.xml





view
plain

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<ScrollView

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/textView"

/>

</ScrollView>

</LinearLayout>



(2)与上面显示图片类似





view
plain

public class MainActivity extends Activity

{

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);



TextView textView = (TextView)this.findViewById(R.id.textView);

try

{

textView.setText(HtmlService.getHtml("http://www.sohu.com"));

}

catch (Exception e)

{

Log.e("MainActivity", e.toString());

Toast.makeText(MainActivity.this, "网络连接失败", 1).show();

}

}

}







view
plain

public class HtmlService

{

public static String getHtml(String path) throws Exception

{

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

conn.setRequestMethod("GET");

conn.setConnectTimeout(5 * 1000);

//通过输入流获取html数据

InputStream inStream = conn.getInputStream();

//得到html的二进制数据

byte[] data = StreamTool.readInputStream(inStream);

String html = new String(data, "gb2312");

return html;

}

}







view
plain

public class StreamTool

{

/**

* 从输入流中获取数据

* @param inStream 输入流

* @return

* @throws Exception

*/

public static byte[] readInputStream(InputStream inStream) throws Exception

{

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int len = 0;

while( (len=inStream.read(buffer)) != -1 )

{

outStream.write(buffer, 0, len);

}

inStream.close();

return outStream.toByteArray();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐