English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Simple way to get images from the Internet in Android programming

This article demonstrates a simple method for Android programming to obtain images from the internet. Shared for everyone's reference, as follows:

To get images from the internet, you first need network access permissions, which will not be written here, as it has been written in previous articles. Also, the layout method will not be written, as it would also be a simple layout without much significance, so let's go directly to the core code:

This is a simple class I use to get images from the internet.

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,

Below is a simple call to the class:

Here is the core code directly:

Bitmap mBitmap;
// Directly obtain the image:
private void RefreshDB() {
  try {
    sendMSG(Declare.START, "正在加载图片......");
    mBitmap = DownFile.getImage(图片地址);
    sendMSG(Declare.STOP, "");
  } catch (Exception e) {
    // TODO Auto-generated catch block
    sendMSG(Declare.ERROR, e.getMessage());
  }
}

Here is a simple scrollbar indicating that the current program is running. It is implemented by sending messages, and the specific implementation of that message is not written, that message has no head this core code implementation.

Execute after loading is complete:

mImageView1.setImageBitmap(mBitmap);

Readers who are interested in more content related to Android can check the special topics on this site: 'Summary of Android Graphics and Image Processing Techniques', 'Android Development入门与进阶教程', 'Summary of Android Debugging Techniques and Common Problem Solving Methods', 'Summary of Android Multimedia Operation Techniques (audio, video, recording, etc.)', 'Summary of Android Basic Component Usage', 'Summary of Android View View Techniques', 'Summary of Android Layout Layout Techniques', and 'Summary of Android Widget Usage'

I hope the description in this article will be helpful to everyone's Android program design.

Declaration: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like