- Back to Home »
- Android »
- How do I download pictures from the Internet
Tuesday, June 26, 2012
If you want to download pictures from the Internet, how to write it?
The following may need to refer to the concept:
How to use the Thread and Handler, as well as some concept Http request.
First XML to define an ImageView with ProgressBar
Then declare a class called DownloadWebPicture
Add getUrlPic method and then the inside
Only need to pass the image URL will return a Bitmap object.
This method is generally explain the web site URL to open a connection, and then get a stream for this connection,
After first obtain the size of the picture, followed by an array loaded the image stream,
Again use of BitmapFactory way to turn into a Bitmap object.
Then, in the declaration of a method
Handler is used to run the Thread when to send a message to the Main Thread know what I download is complete.
And then write a method to obtain the download is complete the picture
Next to the Activity here
When Thread notice Handler to deal with good times, we will ProgressBar hidden
Then downloaded the pictures thrown into the ImageView to complete this program.
Code download
http://uploadingit.com/file/w3vx6fqhpfisvxh3/DownloadWebPictureDemo.zip
How to use the Thread and Handler, as well as some concept Http request.
First XML to define an ImageView with ProgressBar
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http: //schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:id="@+id/progress_bar" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView"/> </FrameLayout>
As a start to let the user know that is currently being read, and will not think that crashes the program shut down. Then declare a class called DownloadWebPicture
public class DownloadWebPicture {}
Add getUrlPic method and then the inside
public synchronized Bitmap getUrlPic(String url) { Bitmap webImg = null; try { URL imgUrl = new URL(url); HttpURLConnection httpURLConnection = (HttpURLConnection) imgUrl.openConnection(); httpURLConnection.connect(); InputStream inputStream = httpURLConnection.getInputStream(); int length = (int) httpURLConnection.getContentLength(); int tmpLength = 512; int readLen = 0,desPos = 0; byte[] img = new byte[length]; byte[] tmp = new byte[tmpLength]; if (length != -1) { while ((readLen = inputStream.read(tmp)) > 0) { System.arraycopy(tmp, 0, img, desPos, readLen); desPos += readLen; } webImg = BitmapFactory.decodeByteArray(img, 0,img.length); if(desPos != length){ throw new IOException("Only read" + desPos +"bytes"); } } httpURLConnection.disconnect(); } catch (IOException e) { Log.e("IOException",e.toString()); } return webImg; }
Here if you do not have Http IOStream the concept, can this method as a black box. Only need to pass the image URL will return a Bitmap object.
This method is generally explain the web site URL to open a connection, and then get a stream for this connection,
After first obtain the size of the picture, followed by an array loaded the image stream,
Again use of BitmapFactory way to turn into a Bitmap object.
Then, in the declaration of a method
public void handleWebPic(final String url, final Handler handler){ new Thread(new Runnable(){ @Override public void run() { bmp = getUrlPic(url); Message msg = new Message(); msg.what = 1; handler.sendMessage(msg); } }).start(); }
This method the incoming picture address, and Handler, Handler is used to run the Thread when to send a message to the Main Thread know what I download is complete.
And then write a method to obtain the download is complete the picture
public Bitmap getImg(){ return bmp; }
Next to the Activity here
private DownloadWebPicture loadPic; private Handler mHandler; private ProgressBar progressBar; private ImageView imageView; private final static String url = "http: //uploadingit.com/file/lltpirkd9pk3jbuw/raccoon.png";
Define some variables and initialize these variables in the onCreate @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); progressBar = (ProgressBar)findViewById(R.id.progress_bar); imageView = (ImageView)findViewById(R.id.imageView); loadPic = new DownloadWebPicture(); mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch(msg.what){ case 1: progressBar.setVisibility(View.GONE); imageView.setImageBitmap(loadPic.getImg()); break; } super.handleMessage(msg); } }; loadPic.handleWebPic(url, mHandler); }
Can see from the program, create a Handler to deal with a Thread lost variables back When Thread notice Handler to deal with good times, we will ProgressBar hidden
Then downloaded the pictures thrown into the ImageView to complete this program.
http://uploadingit.com/file/w3vx6fqhpfisvxh3/DownloadWebPictureDemo.zip