views:

460

answers:

4

I currently have two codes that i am trying to figure out how to mix or write a new code to have a gallery with image doing fill_parent but i can slide sideways to see the other url linked images from my site. here are both parts of code i have:

package com.mandarich.gallerygrid;

import java.io.InputStream; import java.net.URL; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.widget.ImageView;

public class Gallery extends Activity {
/** Called when the activity is first created. */




    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       setContentView(R.layout.main);


       ImageView imgView =(ImageView)findViewById(R.id.ImageView01);
              Drawable drawable = LoadImageFromWebOperations("http://www.mandarichmodels.com/hot-pics/");
              imgView.setImageDrawable(drawable);



}


private Drawable LoadImageFromWebOperations(String url) {
      try
        {


            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        }catch (Exception e) {
            System.out.println("Exc="+e);
            return null;
        }
    }
    }

and here is my gallery code

package com.mandarich.Grid;

import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.os.Bundle; 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.Toast; import android.widget.AdapterView.OnItemClickListener;

public class gridfinal extends Activity {

    private Gallery gallery;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

         gallery = (Gallery) findViewById(R.id.examplegallery);
         gallery.setAdapter(new AddImgAdp(this));

         gallery.setOnItemClickListener(new OnItemClickListener() {
            @SuppressWarnings("unchecked")
            public void onItemClick(AdapterView parent, View v, int position, long id) {
// Displaying the position when the gallery item in clicked
                Toast.makeText(gridfinal.this, "Position=" + position, Toast.LENGTH_SHORT).show();
            }
        });

    }

    public class AddImgAdp extends BaseAdapter {
        int GalItemBg;
        private Context cont;

// Adding images.
        private Integer[] Imgid = {
                R.drawable.cindy, R.drawable.clinton, R.drawable.colin, R.drawable.cybil, R.drawable.david, R.drawable.demi, R.drawable.drew
        };

        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 Imgid.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 imgView = new ImageView(cont);

            imgView.setImageResource(Imgid[position]);
// Fixing width & height for image to display
            imgView.setLayoutParams(new Gallery.LayoutParams(430, 370));
            imgView.setScaleType(ImageView.ScaleType.FIT_XY);
            imgView.setBackgroundResource(GalItemBg);

            return imgView;
    }
}

}

A: 

Here is my code resubmitted was i was trying to ask is there a way to combine the codes or a tutorial on how to make a multiple url gallery of images where i can slide finger sideways and it access the new picture url and displays. i tried droid fu and no luck. here is my codes resubmitted for ya per steve's req. first one is the url link display.

package com.mandarichmodels.button1;

import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;

public class webwithbutton1 extends Activity {
/** Called when the activity is first created. */



    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       setContentView(R.layout.main);


       ImageView imgView =(ImageView)findViewById(R.id.ImageView01);
              Drawable drawable = LoadImageFromWebOperations("http://www.mandarichmodels.com/hot-pics/4.jpg");
              imgView.setImageDrawable(drawable);



}


private Drawable LoadImageFromWebOperations(String url) {
      try
        {


            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        }catch (Exception e) {
            System.out.println("Exc="+e);
            return null;
        }
    }


}

and here is the gallery one

package com.mandarich.Grid;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
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.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class gridfinal extends Activity {

    private Gallery gallery;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

         gallery = (Gallery) findViewById(R.id.examplegallery);
         gallery.setAdapter(new AddImgAdp(this));

         gallery.setOnItemClickListener(new OnItemClickListener() {
            @SuppressWarnings("unchecked")
            public void onItemClick(AdapterView parent, View v, int position, long id) {
// Displaying the position when the gallery item in clicked
                Toast.makeText(gridfinal.this, "Position=" + position, Toast.LENGTH_SHORT).show();
            }
        });

    }

    public class AddImgAdp extends BaseAdapter {
        int GalItemBg;
        private Context cont;

// Adding images.
        private Integer[] Imgid = {
                R.drawable.one, R.drawable.two, R.drawable.three, R.drawable.four, R.drawable.five, R.drawable.six
        };

        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 Imgid.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 imgView = new ImageView(cont);

            imgView.setImageResource(Imgid[position]);
// Fixing width & height for image to display
            imgView.setLayoutParams(new Gallery.LayoutParams(330, 400));
            imgView.setScaleType(ImageView.ScaleType.FIT_XY);
            imgView.setBackgroundResource(GalItemBg);

            return imgView;
    }
}

}
A: 

The idea was to edit your previous post, and not to submit an 'answer' where you repeated yourself! :P You still just posted the whole lot of your code rather than trimming it down to the relevant parts. For example, you don't need to show us your import statements or the boilerplate code that goes with the Base Adapter.

Anyway. I think your problem can be broken down into a few steps:

  1. You need to download images from a website and save them in your application. Your first block of code does a simple version of that. What you really want to do here is save these images into an array.
  2. Once you have your array of images, you can then call them up with in the getView(int position, ...) code, as you've done.

So what you're really asking is how to save those images downloaded into drawables into an array. For now we'll assume that there's a fixed number of images to download. Ideally you'd also multi-thread this so that the downloading happened in the background while a placeholding image was shown until the download was complete, but that's a bit more complicated to do if you've never worked with threads.

Basically you just want to use something like this:

Drawable[] drawablesFromUrl = new Drawable[5];
String[] url = {"first image url", "second image url", ...}

for (int i = 0; i < drawablesFromUrl.length; i++){
    drawablesFromUrl[i] = LoadImageFromWebOperations(url[i]);
}

then just replace the code in your gallery to use drawablesFromUrl rather than Imgid.

Steve H
thanks i got it workin now
A: 

Can someone post the full source codes of this project for me? I really need this. Please send it to me @ [email protected]

Malvin
A: 

I've detailed it here, but here's the code.

main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">
    <Gallery xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
    <ImageView android:id="@+id/ImageView01"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        /> </LinearLayout>

webGalleryExample.java

package za.co.mtn.webGalleryExample; 

import java.io.InputStream;
import java.net.URL; 

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
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 GalleryExample extends Activity {
    private String[] jamCamURLs = {
            “http://path.to.image1.jpg”,
            “http://path.to.image2.jpg”,
            “http://path.to.image3.jpg”,

    };
    Drawable[] drawablesFromUrl = new Drawable[3];
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        for (int i = 0; i < drawablesFromUrl.length; i++){
            drawablesFromUrl[i] = LoadImageFromURL(jamCamURLs[i]);
        }
        final ImageView imgView = (ImageView)findViewById(R.id.ImageView01);
        imgView.setImageDrawable(drawablesFromUrl[0]);
        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this)); 

        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                 imgView.setImageDrawable(drawablesFromUrl[position]);
            }
        });
    }
    private Drawable LoadImageFromURL(String url)
    {
        try
        {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        }catch (Exception e) {
            System.out.println("Exc="+e);
            return null;
        }
    }
    /*public static boolean StoreByteImage(Context mContext, byte[] imageData,
            int quality, String expName) { 

        File sdImageMainDirectory = new File("/sdcard/myImages");
        FileOutputStream fileOutputStream = null;
        String nameFile = null;
        try { 

            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize = 5;
            Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
                    imageData.length,options); 

            fileOutputStream = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + nameFile + ".jpg");
            BufferedOutputStream bos = new BufferedOutputStream(
                    fileOutputStream); 

            myImage.compress(CompressFormat.JPEG, quality, bos); 

            bos.flush();
            bos.close(); 

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        return true;
    }*/ 

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext; 

        public ImageAdapter(Context c) {
            mContext = c;
            TypedArray a = obtainStyledAttributes(R.styleable.JamCam);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.JamCam_android_galleryItemBackground, 0);
            a.recycle();
        } 

        public int getCount() {
            return jamCamURLs.length;
            //return mImageIds.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(mContext); 

            i.setImageDrawable(drawablesFromUrl[position]);
            i.setLayoutParams(new Gallery.LayoutParams(70, 57));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setBackgroundResource(mGalleryItemBackground); 

            return i;
        }
    }
}
Yusufk