Monday, April 30, 2012

Android HTTP connection and Download Images from Server

Android HTTP connection and Download Images from Server 



1.Main.java 
  1. package android.test;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.net.MalformedURLException;  
  5. import java.net.URL;  
  6. import java.net.URLConnection;  
  7. import android.app.Activity;  
  8. import android.content.Context;  
  9. import android.content.res.TypedArray;  
  10. import android.graphics.Bitmap;  
  11. import android.graphics.BitmapFactory;  
  12. import android.os.Bundle;  
  13. import android.view.View;  
  14. import android.view.ViewGroup;  
  15. import android.widget.BaseAdapter;  
  16. import android.widget.Gallery;  
  17. import android.widget.ImageView;  
  18. public class Main extends Activity {  
  19.     private Gallery m_gallery;  
  20.     private String[] m_imageURLs = { "http://www.lvyou114.com/member/1971/UploadFile/200691217738739.jpg",  
  21.             "http://img5.3lian.com/gaoqing/01/02/185.jpg",  
  22.             "http://www.wallcoo.com/nature/Fall_Colors/images/1Fall_Colors_07.jpg",  
  23.             "http://pic1a.nipic.com/2008-10-15/2008101585324870_2.jpg",  
  24.             "http://www.zjj-hc.com/view/jp/%E5%BC%A0%E5%AE%B6%E7%95%8C%E6%99%AF%E8%89%B2.gif" };  
  25.     /** Called when the activity is first created. */  
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.main);  
  30.         m_gallery = (Gallery) findViewById(R.id.myGallery);  
  31.         m_gallery.setAdapter(new MyInternetGalleryAdapter(Main.this));  
  32.     }  
  33.     class MyInternetGalleryAdapter extends BaseAdapter {  
  34.         private Context m_context;  
  35.         private int m_nGalleryItemBG;  
  36.         public MyInternetGalleryAdapter(Context c) {  
  37.             m_context = c;  
  38.             TypedArray arr = m_context.obtainStyledAttributes(R.styleable.myGalleryStyleable);  
  39.             m_nGalleryItemBG = arr.getResourceId(R.styleable.myGalleryStyleable_android_galleryItemBackground, 0);  
  40.             arr.recycle();  
  41.         }  
  42.         @Override  
  43.         public int getCount() {  
  44.             return m_imageURLs.length;  
  45.         }  
  46.         @Override  
  47.         public Object getItem(int position) {  
  48.             return position;  
  49.         }  
  50.         @Override  
  51.         public long getItemId(int position) {  
  52.             return position;  
  53.         }  
  54.         @Override  
  55.         public View getView(int position, View convertView, ViewGroup parent) {  
  56.             ImageView imgView = new ImageView(m_context);  
  57.             try {  
  58.                 URL url = new URL(m_imageURLs[position]);  
  59.                 URLConnection conn;  
  60.                 conn = url.openConnection();  
  61.                 conn.connect();  
  62.                 InputStream in = conn.getInputStream();  
  63.                 Bitmap bmp = BitmapFactory.decodeStream(in);  
  64.                 in.close();  
  65.                 imgView.setImageBitmap(bmp);  
  66.             } catch (MalformedURLException e) {  
  67.                 e.printStackTrace();  
  68.             } catch (IOException e) {  
  69.                 e.printStackTrace();  
  70.             }  
  71.             imgView.setScaleType(ImageView.ScaleType.FIT_XY);  
  72.             imgView.setLayoutParams(new Gallery.LayoutParams(200150));  
  73.             imgView.setBackgroundResource(m_nGalleryItemBG);  
  74.             return imgView;  
  75.         }  

  76.         public float getScale(boolean focused, int offset) {  
  77.             /* Formula: 1 / (2 ^ offset) */  
  78.             return Math.max(01.0f / (float) Math.pow(2, Math.abs(offset)));  
  79.         }  
  80.     }  
  81. }  


2. AndroidManifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="android.test"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="7"></uses-sdk>  
  7.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  8.         <activity android:name=".Main"  
  9.                   android:label="@string/app_name">  
  10.             <intent-filter>  
  11.                 <action android:name="android.intent.action.MAIN" />  
  12.                 <category android:name="android.intent.category.LAUNCHER" />  
  13.             </intent-filter>  
  14.         </activity>  
  15.     </application>  
  16.     <uses-permission android:name="android.permission.INTERNET"/>  
  17. </manifest>  


please leave your comets 

No comments: