android url 방식 열기

2736 단어 android
android url 방식 열기
방식 1:
Uri uri = Uri.parse("http://www.baidu.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

방식 2:
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 [email protected] * @version 1.0 */ public class URLTest extends Activity { ImageView show; // Bitmap bitmap; Button urlBtn; //handler thread Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if(msg.what == 0x123) { // ImageView show.setImageBitmap(bitmap); } } }; @Override public 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(); } }

좋은 웹페이지 즐겨찾기