图片库与网址图片

时间:2011-05-14 12:17:48

标签: android

我想创建一个图片库,从网址中获取图片..这是我的代码但不起作用...

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;


public class gallery extends Activity {
    private Gallery gallery;

    private ImageView imgView;


    private String[] myRemoteImages = {

            "http://www.kostas-menu.gr/chania/venizelou.jpg", 
            "http://www.kostas-menu.gr/chania/faros.jpg",
            "http://www.kostas-menu.gr/chania/giali.jpg",
            "http://www.kostas-menu.gr/chania/kipos.jpg"

    };


    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.gallery);


        imgView = (ImageView)findViewById(R.id.ImageView01);

        imgView.setImageResource(myRemoteImages[0]);


         gallery = (Gallery) findViewById(R.id.examplegallery);

         gallery.setAdapter(new AddImgAdp(this));


         gallery.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView parent, View v, int position, long id) {

                imgView.setImageResource(myRemoteImages[position]);

            }

        });


    }


    public class AddImgAdp extends BaseAdapter {

        int GalItemBg;

        private Context cont;


        public AddImgAdp(Context c) {

            cont = c;

            TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);

            GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);

            typArray.recycle();

        }


        public int getCount() {

            return myRemoteImages.length;

        }


        public Object getItem(int position) {

            return position;

        }


        public long getItemId(int position) {

            return position;

        }


        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(this.myContext);
            try {
                                /* Open a new URL and get the InputStream to load data from it. */
                                URL aURL = new URL(myRemoteImages[position]);
                                URLConnection conn = aURL.openConnection();
                                conn.connect();
                                InputStream is = conn.getInputStream();
                                /* Buffered is always good for a performance plus. */
                                BufferedInputStream bis = new BufferedInputStream(is);
                                /* Decode url-data to a bitmap. */
                                Bitmap bm = BitmapFactory.decodeStream(bis);
                                bis.close();
                                is.close();
                                /* Apply the Bitmap to the ImageView that will be returned. */
                                i.setImageBitmap(bm);
                        } catch (IOException e) {
                                i.setImageResource(R.drawable.error);
                                Log.e("DEBUGTAG", "Remtoe Image Exception", e);
                        }

    }


}

错误是我无法使用imgView.setImageResource(myRemoteImages[0]);表示字符串...请问嗨?

1 个答案:

答案 0 :(得分:3)

我建议您将此LazyLoader用于从网址Lazy load of images in ListView获取的图片。

对于您的情况,我的意思是在图库视图中从Web加载图像,使用示例链接中给出的相同适配器。