Android Internet access method for obtaining images on the network
To obtain images on the network, you must first have the network access permission. This is not written anymore. It has been written in previous articles, and the layout method is no longer written, writing is also a simple layout. It doesn't make much sense to directly use the core code:
This is a simple class I use to obtain online images.
Public static Bitmap getImage (String Url) throws Exception {
Try {
URL url = new URL (Url );
String responseCode = url. openConnection (). getHeaderField (0 );
If (responseCode. indexOf ("200") <0)
Throw new Exception ("the image file does not exist or the path is incorrect. Error Code:" + responseCode );
Return BitmapFactory. decodeStream (url. openStream ());
} Catch (IOException e ){
// TODO Auto-generated catch block
Throw new Exception (e. getMessage ());
}
}
This class returns a Bitmap object,
The following is a simple call to the class:
Directly run the Code:
Bitmap mBitmap;
// Directly retrieve the image:
Private void RefreshDB (){
Try {
SendMSG (Declare. START, "loading image ......");
MBitmap = DownFile. getImage (image address );
SendMSG (Declare. STOP ,"");
} Catch (Exception e ){
// TODO Auto-generated catch block
SendMSG (Declare. ERROR, e. getMessage ());
}
A simple scroll bar is provided here, indicating that the current program is being executed and implemented by sending messages. The core code is not written anymore.
Run the following command after loading:
MImageView1.setImageBitmap (mBitmap );