How does the Android client load images from the web server

In the development of the android project, I believe that many small partners will encounter when the app needs to load pictures, and many beginners will feel a headache. Here I will talk about the method of loading pictures in android in detail.

Whether you load your own pictures or pictures on the Internet, the biggest difference is to get the link to the picture. . .

First, make a web-side service, publish the picture in Jboss or Tomcat, and get the picture through the link.

       Here is a case of dynamically obtaining pictures, and save pictures from other addresses to the Jboss project directory after obtaining them:

//Code snippet, get a picture from the folder and store it in the jboss project path

 byte[]  buffer=readImgData(pathStr);
File  imgfile=new File("F:\\jboss\\server\\default\\deploy\\MyFirstWebServices.war\\temp.jpg");
       //判断文件是否存在
       if(!imgfile.exists()){
        imgfile.delete();
       }
       FileOutputStream  output=new FileOutputStream(imgfile);
       output.write(buffer);
       output.flush();
       output.close();

// Get an image from an image path 

public byte[] readImgData(String filepath){
  try{
    File  file = new File(filepath);
    FileInputStream  input = new FileInputStream(file);
    ByteArrayOutputStream  output = new ByteArrayOutputStream();
    byte[]  buffer=new byte[1024];
    int len = 0;
    while ((len = input.read(buffer))!=-1) {
     output.write(buffer, 0, len);
    }

    input.close();
    return output.toByteArray();
    }catch(Exception e){
     e.printStackTrace();
    }  
    return null;
   }

 After the project is deployed, the image information can be accessed by publishing the project as follows:

http://localhost:8080/MyFirstWebServices/temp.jpg;

Second, the code for displaying pictures in android.

1. The code snippet in imgshow.xml, just put an imageview

       <ImageView
                    android:id="@+id/sat_show_image"
                    android:layout_width="400dp"
                    android:layout_height="200dp" 
     android:layout_gravity="center"
     android:scaleType="fitXY" 
                    android:layout_centerVertical="true"
     android:layout_span="2"
     />

2. imgshow.java code snippet

 private ImageView  satimg;
 String imageUrl="http://localhost:8080/MyFirstWebServices/temp.jpg";
  private Handler handler = new Handler(){
         public void handleMessage(Message msg) {
             switch (msg.what) {
             case 0:
                 Bitmap bit = (Bitmap) msg.obj;
              satimg.setImageBitmap(bit);
              satimg.setScaleType(ImageView.ScaleType.FIT_XY);
                 break;
             default:
                 break;
             }
         };

     };

@Override
 protected void onCreate(Bundle savedInstanceState)
 {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.imgshow);

  satimg = (ImageView)findViewById(R.id.sat_show_image);

  record_img. setOnClickListener( new OnClickListener()
  {
   
   @Override
   public void onClick(View arg0)
   {
    // TODO Auto-generated method stub
                 // New thread loads picture information and sends it to the message queue 
                 new Thread(new Runnable() { 
  
                     @Override 
                     public void run() { 
                         // TODO Auto-generated method stub 
                         Bitmap bmp = getURLimage(imageUrl); 
                         Message msg = new Message(); 
                         msg.what = 0; 
                         msg.obj = bmp; 
                         System.out.println("000"); 
                         handler.sendMessage(msg); 
                     } 
                 }).start(); 
     }
   });
 }

 

  //Load image 
    public Bitmap getURLimage(String url) { 
        Bitmap bmp = null; 
        try { 
            URL myurl = new URL(url); 
            // Get connection 
            HttpURLConnection conn = (HttpURLConnection) myurl.openConnection(); 
            conn.setConnectTimeout(6000) ;//Set the timeout 
            conn.setDoInput(true); 
            conn.setUseCaches(false);//Do not cache 
            conn.connect(); 
            InputStream is = conn.getInputStream();//Get the data stream of the picture 
            bmp = BitmapFactory.decodeStream (is); 
            is.close(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return bmp; 
    } 

 

Complete the above steps to display the image in the app.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326359021&siteId=291194637