如何设置点击图库图像的URL?

时间:2011-08-05 16:03:00

标签: android

我正在从服务器上的文本文档中检索url字符串。

图像被检索后,使用ImageAdapter将它们设置为图库。

 public class ImageAdapter extends BaseAdapter {

           /** The parent context */
       private Context myContext;public ImageAdapter() {
            // TODO Auto-generated constructor stub
            }


                /** URL-Strings to some remote images. */
        public String[] myRemoteImages = {imageUrl,imageUrl2,imageUrl3,imageUrl4};  

        private String[] mImageURLs = {
              "http://www.google.com",
              "http://www.google.com"};






                /** Simple Constructor saving the 'parent' context. */
         public ImageAdapter(Context c) { this.myContext = c; }





                /** Returns the amount of images we have defined. */
          public int getCount() { 
                return this.myRemoteImages.length;
                }




                /* Use the array-Positions as unique IDs */
           public Object getItem(int position) { 
                return position; 
                }


           public long getItemId(int position) { 
                    return position; 
                    }


                /** Returns a new ImageView to
                * be displayed, depending on
                * the position passed. */
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView i = new ImageView(this.myContext);

            //  i.setTag(mImageURLs[position]);


                try {


            URL aURL = new URL(myRemoteImages[position]);
            Log.v("ImageLoader", "Remote images set");


            URI imageUri = null;

            //Setting the Uri of aURL to imageUri.
            try {
            imageUri = aURL.toURI();

            } catch (URISyntaxException e1) {
            // TODO Auto-generated catch block
                e1.printStackTrace();
                        }



                //Testing to see if images are already in cache, if not then we load the images from the web and save them to the cache.
           if (new File(new File(myContext.getCacheDir(), "thumbnails"), "" + imageUri.hashCode()).exists())
                           {

        Log.v("Loader", "File exists in cache. Now pulling from the cache");

        String cachFile = myContext.getCacheDir() +"/thumbnails/"+imageUri.hashCode();
        FileInputStream fis;

        try {
        fis = new FileInputStream(cachFile);
        Bitmap bm = BitmapFactory.decodeStream(fis); 
        i.setImageBitmap(bm);

现在问题是如何静态地为每个图像设置URL。这意味着当图像发生变化时,网址会发生变化。我怎么可能能够控制每个图像设置的URL?

例如。单击图像时,它会将Web浏览器打开为为该特定图像设置的URL。

一周之后,图像会发生变化,我怎样才能更改与之关联的网址?

编辑:我在使用字符串的数组列表时遇到错误。

 08-05 16:56:50.748: ERROR/AndroidRuntime(646): java.lang.ArrayIndexOutOfBoundsException: index=2 length=2
 08-05 16:56:50.748: ERROR/AndroidRuntime(646):     at com.fttech.gameIT.MainMenu$ImageAdapter.getView(MainMenu.java:379)
08-05 16:56:50.748: ERROR/AndroidRuntime(646):     at android.widget.Gallery.makeAndAddView(Gallery.java:748)
08-05 16:56:50.748: ERROR/AndroidRuntime(646):     at android.widget.Gallery.fillToGalleryRight(Gallery.java:700)
08-05 16:56:50.748: ERROR/AndroidRuntime(646):     at android.widget.Gallery.layout(Gallery.java:631)
08-05 16:56:50.748: ERROR/AndroidRuntime(646):     at android.widget.Gallery.onLayout(Gallery.java:339)
08-05 16:56:50.748: ERROR/AndroidRuntime(646):     at android.view.View.layout(View.java:9330)
08-05 16:56:50.748: ERROR/AndroidRuntime(646):     at android.view.ViewGroup.layout(ViewGroup.java:3795)

错误指向我......

    public View getView(int position, View convertView, ViewGroup parent) {
                ImageView i = new ImageView(this.myContext);




                try {


            URL aURL = new URL(myRemoteImages[position]);
            Log.v("ImageLoader", "Remote images set");
            //It points me here  i.setTag(mImageURLs[position]);                    

            URI imageUri = null;

2 个答案:

答案 0 :(得分:1)

使用String对象创建第二个ArrayList(如果有类,则创建链接),每个Image将通过适配器在该Array中表示自己。

例如:

图像适配器包含[Image0,Image1,Image2,Image3,Image4], 带链接的ArrayList包含[Link0,Link1,Link2,Link3,Link4]。

因此,每当有人点击图像时,您都会抓住“位置”,并使用它来从ArrayList中获取链接。

希望这对你有帮助

答案 1 :(得分:0)

您需要ArrayList<String>。并且每个Image都将在该数组中表示自己。