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

Android Programming: Detailed Explanation of Displaying Images from the Internet

This article describes the method of displaying images from the network in Android programming. Shared for everyone's reference, as follows:

To display images from the network in Android, you need to find the image address according to the url first, then convert the image to Java's InputStream, and then convert the InputStream to BitMap. BitMap can be directly displayed in the ImageView in android. This is the idea of displaying images from the network, and it is very simple to implement. Let's take a look at the process of implementation.

First, add the permission to access the Internet to the program in AndroidManifest.xml:

<uses-permissionandroid:name="android.permission.INTERNET" />

Then add an ImageView to the layout file to display images from the network:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string"/hello" />
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@"+id/imageView" />
</LinearLayout>

Write code in the Activity of the main program to get an image from the network, convert it to InputStream, and then convert it to a Bitmap that can be displayed in ImageView.

package com.image;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class NetImageActivity extends Activity {
  /** Called when the activity is first created. */
   String imageUrl = "http://content.52pk.com/files/100623/2230_102437_1_lit.jpg";
   Bitmap bmImg;
   ImageView imView;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    imView = (ImageView) findViewById(R.id.imageView);
    imView.setImageBitmap(returnBitMap(imageUrl));
  }
  public Bitmap returnBitMap(String url){
    URL myFileUrl = null;
    Bitmap bitmap = null;
    try {
      myFileUrl = new URL(url);
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }
    try {
      HttpURLConnection conn = (HttpURLConnection) myFileUrl
       .openConnection();
      conn.setDoInput(true);
      conn.connect();
      InputStream is = conn.getInputStream();
      bitmap = BitmapFactory.decodeStream(is);
      is.close();
    } catch (IOException e) {
       e.printStackTrace();
    }
       return bitmap;
  }
}

Then run the program to display pictures on the network.

Running effect:

PS: For detailed content of AndroidManifest.xml permission control, you can refer to the online tool on this site:

Comprehensive Description of AndroidManifest Function and Permissions:
http://tools.jb51.net/table/AndroidManifest

Readers who are interested in more about Android-related content can check the special topics on this site: 'Summary of Android Graphics and Image Processing Techniques', 'Android Development Tutorial for Beginners and Advanced', '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 content described in this article will be helpful to everyone in Android program design.

Declaration: The content of this article is from the Internet, 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 edited by humans, 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 for reporting. Provide relevant evidence, and once verified, this site will immediately delete the infringing content.)

You May Also Like