package com.powenko.Tutorial_UI_Image_displayURL; import java.net.URL; import java.net.URLConnection; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class Tutorial_UI_Image_displayURLActivity extends Activity { private Button mButton1; private Button mButton2; private ImageView mImageView; private Bitmap bm; private String path="http://www.powenko.com/en/wp-content/uploads/2011/08/device-2011-08-22-055330.png"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton1 =(Button) findViewById(R.id.myButton1); mButton2 =(Button) findViewById(R.id.myButton2); mImageView = (ImageView) findViewById(R.id.myImage); mButton2.setEnabled(false); mButton1.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { setImage(path,1); } }); mButton2.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { try { setImage(path,2); } catch (Exception e) { showDialog("error"); bm = null; mImageView.setImageBitmap(bm); mButton2.setEnabled(false); e.printStackTrace(); } } }); } private void setImage(String path,int type) { try { URL url = new URL(path); URLConnection conn = url.openConnection(); conn.connect(); if(type==1) { bm = BitmapFactory.decodeStream(conn.getInputStream()); mImageView.setImageBitmap(bm); mButton2.setEnabled(true); } else if(type==2) { Tutorial_UI_Image_displayURLActivity.this.setWallpaper(conn.getInputStream()); bm = null; mImageView.setImageBitmap(bm); mButton2.setEnabled(false); showDialog("ok."); } } catch (Exception e) { showDialog("error!"); bm = null; mImageView.setImageBitmap(bm); mButton2.setEnabled(false); e.printStackTrace(); } } private void showDialog(String mess){ new AlertDialog.Builder(Tutorial_UI_Image_displayURLActivity.this).setTitle("Message") .setMessage(mess) .setNegativeButton("process...", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .show(); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/layout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <TextView android:id="@+id/myText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textSize="16sp" android:layout_x="20px" android:layout_y="12px" /> <Button android:id="@+id/myButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="download" android:layout_x="70px" android:layout_y="102px" /> <Button android:id="@+id/myButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="wallpage" android:layout_x="150px" android:layout_y="102px" /> <ImageView android:id="@+id/myImage" android:layout_width="200dip" android:layout_height="200dip" android:layout_x="20px" android:layout_y="152px" /> </AbsoluteLayout>