功能简介:从网络中下载一个图片,图片的格式为指定的格式,将图片保存在SDcard的根目录中,同时显示出来。 源代码: 代码需要进一步改进,忘高手指点 package com.acer.download; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.content.DialogInterface; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; public class Download extends Activity { /** Called when the activity is first created. */ private EditText url; private Button download; private ProgressBar progress; private String url_address; private Bitmap bitmap=null; private FrameLayout frameLayout; private int file_length; private int downloadfilesize=0; private String downloadPath=Environment.getExternalStorageDirectory().getAbsolutePath(); private String Url_address; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViews(); setListener(); } //get the widget private void findViews(){ url=(EditText)this.findViewById(R.id.url); download=(Button)this.findViewById(R.id.download); frameLayout=(FrameLayout)this.findViewById(R.id.frameLayout); progress=(ProgressBar)this.findViewById(R.id.progress); progress.setVisibility(View.GONE); progress.incrementProgressBy(1); } //handle the users action private void setListener(){ download.setOnClickListener(download_start); } //send the message to hander function private void sendMsg(int flag) { Message msg = new Message(); msg.what = flag; handler.sendMessage(msg); } //view the picture private Handler handler=new Handler(){ public void handleMessage(Message msg){ switch(msg.what) { case 0: progress.setMax(file_length); break; case 1: progress.setProgress(downloadfilesize); break; case 2: //view the picture which read from the sdcard String my_image_path="/sdcard/image.jpg"; BitmapFactory.Options options=new BitmapFactory.Options(); bitmap=BitmapFactory.decodeFile(my_image_path,options); ImageView view=(ImageView)frameLayout.findViewById(R.id.image); view.setImageBitmap(bitmap); progress.setVisibility(view.GONE); //dismiss the progressBar break; } super.handleMessage(msg); } }; // the function is to test the URL is ends with the given Strings or not private boolean formatValid(String str){ String str1; str1=str.toLowerCase(); if(str1.endsWith(".png")||str1.endsWith(".jpg")||str1.endsWith(".gif")||str1.endsWith(".bmp")) return true; return false; } //handle the button's click public OnClickListener download_start=new Button.OnClickListener(){ public void onClick(final View view){ progress.setVisibility(view.VISIBLE); //view the progressBar progress.setProgress(0); //test the HTTP_URL is right or not String myUrl=url.getText().toString(); if(formatValid(myUrl)) { Url_address=myUrl; } else { Toast.makeText(Download.this,"please input the right URl_address!", Toast.LENGTH_SHORT).show(); return; } //connect to the server and get the resource of picture new Thread(){ public void run(){ try{ //start the HTTP connection URL url_address=new URL(Url_address); HttpURLConnection conn=(HttpURLConnection)url_address.openConnection(); conn.setDoInput(true); conn.connect(); InputStream inputStream=conn.getInputStream(); file_length=conn.getContentLength(); if (file_length <= 0) throw new RuntimeException(" can not get the inputStream "); if (inputStream == null) throw new RuntimeException("stream is null"); FileOutputStream output=new FileOutputStream("/sdcard/"+"image.jpg"); //storage the download file byte[] data=new byte[4096]; int count=0; sendMsg(0); //initialize the progressBar do { count= inputStream.read(data); if (count == -1) { break; } output.write(data, 0, count); downloadfilesize+= count; sendMsg(1); //update the progressBar } while (true); sendMsg(2); //view the picture }catch(MalformedURLException e1){ e1.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start(); }//end of onClick() }; }//end of the Download layout中的代码: <?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" > <EditText android:id="@+id/url" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> <Button android:id="@+id/download" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="start the download"> </Button> <ProgressBar android:id="@+id/progress" style="?android:attr/progressBarStyleHorizontal" mce_style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ProgressBar> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerInside" android:padding="2dp"> </ImageView> </FrameLayout> </LinearLayout>