android 打开 url 方式

方式一:

Uri uri = Uri.parse("http://www.baidu.com");Intent intent = new Intent(Intent.ACTION_VIEW, uri);startActivity(intent);

方式二:

package org.crazyit.net;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.widget.Button;import android.widget.ImageView;import java.io.InputStream;import java.io.OutputStream;import java.net.URL;/** * Description: * 
site: crazyit.org *
Copyright (C), 2001-2014, Yeeku.H.Lee *
This program is protected by copyright laws. *
Program Name: *
Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */public class URLTest extends Activity{ImageView show;// 代表从网络下载得到的图片Bitmap bitmap;Button urlBtn;//handler 机制必须结合 thread 来使用Handler handler = new Handler(){@Overridepublic void handleMessage(Message msg){if(msg.what == 0x123){// 使用ImageView显示该图片show.setImageBitmap(bitmap);}}};@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);show = (ImageView) findViewById(R.id.show);//urlBtn = (Button)findViewById(R.id.url_btn);//urlBtn.setOnClickListener(new View.OnClickListener() {//@Override//public void onClick(View v) {//try//{//Uri uri = Uri.parse("http://www.baidu.com");//Intent intent = new Intent(Intent.ACTION_VIEW, uri);//startActivity(intent);//}//catch (Exception e)//{//e.printStackTrace();//}//}//});new Thread(){public void run(){try{// 定义一个URL对象URL url = new URL("http://www.crazyit.org/"+ "attachments/month_1008/20100812_7763e970f"+ "822325bfb019ELQVym8tW3A.png");// 打开该URL对应的资源的输入流InputStream is = url.openStream();// 从InputStream中解析出图片bitmap = BitmapFactory.decodeStream(is);// 发送消息、通知UI组件显示该图片handler.sendEmptyMessage(0x123);is.close();// 再次打开URL对应的资源的输入流is = url.openStream();// 打开手机文件对应的输出流OutputStream os = openFileOutput("crazyit.png", MODE_WORLD_READABLE);byte[] buff = new byte[1024];int hasRead = 0;// 将URL对应的资源下载到本地while((hasRead = is.read(buff)) > 0){os.write(buff, 0 , hasRead);}is.close();os.close();}catch (Exception e){e.printStackTrace();}}}.start();}}

更多相关文章

  1. Android中自定义Dialog常用的两种方式
  2. android图片缩放手势检测类--ScaleGestureDetector
  3. android图片压缩方法
  4. android ListView SimpleAdapter 带图片
  5. android 资源获取
  6. Android创建和删除桌面快捷方式
  7. Android之OnGestureListener实现图片的左右滑动
  8. Android中创建和检测快捷方式
  9. 图片压缩保存读取操作

随机推荐

  1. Android(安卓)资源加载机制详解
  2. Android之ListActivity:布局与数据绑定
  3. Android(安卓)KTX举例
  4. android 支付宝支付
  5. Vitamio简要介绍
  6. android串口通信
  7. Android中解析XML
  8. Android(安卓)button原理 转载
  9. Android图片异步加载之Android-Universal
  10. Android(安卓)之 Looper Handler Message