I want to get an image from sd card and place it in gallery view.For that i had converted that image to bitmap but it shows certain errors..
code for conversion of the image.. I am attaching the complete code it showing a null pointer exception
private Gallery gallery;
private ImageView imgView;
int position;
private byte[] data = { };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
System.out.println("Enter the activity");
try{
String filepath = "/sdcard/";
File imagefile = new File(filepath + "abcdoutput.jpg");
FileInputStream fis = new FileInputStream(imagefile);
Bitmap bi = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
System.out.println("cnvrtn");
}
catch(Exception e) {
e.printStackTrace();
}
Bitmap bitmapimage = BitmapFactory.decodeByteArray(data, 0, data.length);
position = 0;
imgView = (ImageView) findViewById(R.id.ImageView01);
imgView.setImageBitmap(bitmapimage);
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(data[position]);
DisplayImage.this.position = position;
}
});
System.out.println("Enter the activity//////////");
imgView.setOnLongClickListener(new View.OnLongClickListener() {
#SuppressWarnings("deprecation")
public boolean onLongClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(
DisplayImage.this).create();
alertDialog.setTitle("Confirmation");
alertDialog
.setMessage("Do you want to set this image as wallaper?");
alertDialog.setButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Bitmap bitmap = BitmapFactory.decodeResource(
getResources(), data[position]);
try {
DisplayImage.this.setWallpaper(bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("Gallery Example", "Image setted.");
}
});
alertDialog.show();
return true;
}
});
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = null;
GalItemBg = typArray.getResourceId(
0, 0);
typArray.recycle();
}
public int getCount() {
return data.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(data[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(100, 100));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
Use this to get the image back from the array and set it:
Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length);
imgView.setImageBitmap(bitmap);
try and simple with one line code
String imagePath=Environment.getExternalStorageDirectory().toString()+"/abcdoutput.jpg";
imgView.setImageDrawable(Drawable.createFromPath("imagePath"));
Related
In my application i add images to listview and transfer those images to servelet after conversion into string. But deleted image from listview also transfer. It removes from listview, but string is transfering to servelet. This is annoying . I am not getting where to add these lines in BaseAdapter.
Bitmap image=(Bitmap)getItem(position);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
//encode image
byte[] b = bytes.toByteArray();
encodedImageString = Base64.encodeToString(b, Base64.DEFAULT);
//encodedImageString is sent to servlet, it's showing deleted image string also
Here is a complete code of BaseAdapter
public class MyAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> imageHashMap=new ArrayList<HashMap<String,String>>();
private LayoutInflater mInflater;
public MyAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// return myItems.size();
return images.size();
}
public Bitmap getItem(int position) {
return images.get(position);
//return myItems.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
final Bitmap image=(Bitmap)getItem(position);
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.imageview2, null);
holder.image = (ImageView) convertView
.findViewById(R.id.imageView2);
holder.Delete=(Button)convertView.findViewById(R.id.buttonClose);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
BitmapFactory.Options factoryOptions = new BitmapFactory.Options();
int imageWidth = factoryOptions.inDensity=70;
int imageHeight = factoryOptions.inDensity=65;
Bitmap Scaled = Bitmap.createScaledBitmap(images.get(position), imageWidth, imageHeight, true);
holder.image.setImageBitmap(Scaled);
holder.image.setTag(position);
String me= holder.image.getTag().toString();
holder.Delete.setTag(position);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
//encode image
final ArrayList<HashMap<String, String>> imageHashMap=new ArrayList<HashMap<String,String>>();
byte[] b = bytes.toByteArray();
encodedImageString = Base64.encodeToString(b, Base64.DEFAULT);
// StringImages.add(encodedImageString);
imageHashMap.putString(String.valueOf(image.getTag()), encodedImageString);
holder.image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
final Dialog imgDialog = new Dialog(view.getContext(),android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
imgDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
imgDialog.setCancelable(false);
// layout imageview2 is used because when i use simple imageview layout dialogue with imageview and closebutton,
// every taken image at instance will not be shown in dialogue.
imgDialog.setContentView(R.layout.imageview);
Button btnClose = (Button)imgDialog.findViewById(R.id.btnIvClose);
ImageView ivPreview = (ImageView)imgDialog.findViewById(R.id.image1);
ivPreview.setImageBitmap(images.get(position));
btnClose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
imgDialog.dismiss();
}
});
imgDialog.show();
myAdapter.notifyDataSetChanged();
listviewattachment.setSelection(myAdapter.getCount()+1 );
}
});
holder.Delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
int tag = (Integer) view.getTag();
if (tag != (images.size() )) {
images.remove(tag);
Log.d("GCM", "Item removed from " + tag);
myAdapter.notifyDataSetChanged();
}
if(imageHashMap.contains(tag)) {
imageHashMap.remove(tag); }
}
});
return convertView;
}
}
For an example, using hashmap look at this:
Bitmap Scaled = Bitmap.createScaledBitmap(images.get(position), imageWidth,imageHeight,true);
holder.image.setImageBitmap(Scaled);
holder.image.setTag(position);
holder.Delete.setTag(position);
Bitmap image=(Bitmap)getItem(position);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
//encode image
byte[] b = bytes.toByteArray();
encodedImageString = Base64.encodeToString(b, Base64.DEFAULT);
StringImages.add(encodedImageString) -----> Instead of this line, use a hashmap like this
imageHashMap.put(String.valueOf(image.getTag()), encodedImageString);
and in the delete.onclicklistener code as you are removing image from images, like images.remove(image),
add these lines
if(imageHashMap.contains(image.getTag())) {
imageHashMap.remove(image.getTag());
}
I hope this helps.
PS: declare the hashmap in the constructor before you use it, else you might get a null pointer exception
I have an application which allow to download 3 images and show them into a gallery
In order to do this , i use a thread in order to download the images and a handler in order to put the images into the gallery
The problem is that the images are not displayed(imageviews are empty) into the gallery (even if i see 3 imageview for my 3 images)
I do not know if this is because the connection is very slow (I develop on a mobile) or if it's because the ui is displayed before images are downloaded
Thank you very much for your help
public class ChoiceLanguage extends Activity {
private TextView nomLangue;
private ArrayList<Language> listeLangues;
private ArrayList<String> listImages;
private Gallery gallery;
private String URL="******************";
private Drawable mNoImage;
Bitmap bitmap;
ImageView imgView = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choice_language);
listeLangues= getIntent().getParcelableArrayListExtra("listeLangues");
listImages=buildListImages();
gallery = (Gallery) findViewById(R.id.galleryImg);
gallery.setAdapter(new AddImgAdp(this));
gallery.setSpacing(10);
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
System.out.println("URL "+ listImages.get(position) );
}
});
}
private InputStream openHttpConnection(String urlString) throws IOException {
InputStream in = null;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
conn.connect();
in=conn.getInputStream();
return in;
}
private Bitmap downloadImage( ImageView iView, String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedInputStream bis ;
try {
in = openHttpConnection(url);
bis= new BufferedInputStream(in, 8192);
bitmap = BitmapFactory.decodeStream(bis);
bis.close();
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
private ArrayList<String> buildListImages() {
ArrayList<String> listImg = new ArrayList<String>();
for(Language l : listeLangues) {
listImg.add(URL+l.getImagePath());
}
return listImg;
}
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 listImages.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
imgView = new ImageView(cont);
} else {
imgView = (ImageView)convertView;
}
ThreadDownload progressDl= new ThreadDownload(position);
progressDl.start();
imgView.setLayoutParams(new Gallery.LayoutParams(150, 150));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
private class SetImg extends Handler {
public void handleMessage(Message msg) {
imgView.setImageBitmap(bitmap);
}
}
private class ThreadDownload extends Thread {
SetImg img= new SetImg();
int position ;
public ThreadDownload(int position)
{
super();
this.position=position;
}
public void run() {
bitmap = downloadImage(imgView,listImages.get(position));
img.sendEmptyMessage(0);
}
}
}
You can make use of SmartImageView, it is a drop-in replacement for Android’s standard ImageView which additionally allows images to be loaded from URLs or the user’s contact address book. Images are cached to memory and to disk for super fast loading.
https://github.com/loopj/android-smart-image-view
public class MainActivity extends Activity implements ViewSwitcher.ViewFactory, OnItemSelectedListener {
private Gallery gallery;
private ImageSwitcher imageSwitcher;
private ImageAdapter ia;
private int res_id;
private Bitmap mBitmap;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery = (Gallery) findViewById(R.id.gallery);
imageSwitcher = (ImageSwitcher) findViewById(R.id.image_switcher);
imageSwitcher.setFactory(this);
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
ia = new ImageAdapter(this);
gallery.setAdapter(ia);
//Event listener
gallery.setOnItemSelectedListener(this);
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_save:
mBitmap = BitmapFactory.decodeResource(getResources(),res_id);
String filename = "barca_"+res_id+".jpg";
File sd = Environment.getExternalStorageDirectory();
String full_path=sd.getAbsolutePath()+"/BarcaWallpapers/";
File temp=new File(full_path);
temp.mkdirs();
File dest = new File(temp,filename);
try {
FileOutputStream out = new FileOutputStream(dest);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
Toast.makeText(this, "Saved in Folder "+full_path, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "error", Toast.LENGTH_LONG).show();
}
return true;
case R.id.menu_set_wallpapaer:
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
try{
wallpaperManager.setResource(res_id);
Toast.makeText(this,"Wallpaper Set", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Toast.makeText(this, "error setting wallpaper", Toast.LENGTH_SHORT).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int imageResourceId = (Integer) ia.getItem(position);
res_id=imageResourceId;
imageSwitcher.setImageResource(imageResourceId);
}
public View makeView() {
ImageView i = new ImageView(this);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
return i;
}
private class ImageAdapter extends BaseAdapter {
private Context context;
private ImageAdapter(Context context) {
this.context = context;
}
private int[] IMAGE_IDS = {
R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6,
};
public int getCount() {
return IMAGE_IDS.length;
}
public Object getItem(int position) {
return IMAGE_IDS[position];
}
public long getItemId(int position) {
return IMAGE_IDS[position];
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new Gallery.LayoutParams(120,120));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(IMAGE_IDS[position]);
return imageView;
}
}
}
I have the above code for a wallpaper app. The activity xml layout file contains a gallery view and a imageswitcher below it in which the user can browse and navigate through the gallery and the corresponding image opens up below in the imageswitcher. User can then set the image as a wallpaper or save to sd card. The above code works perfectly for small images, but fails miserably for large images like the one I intend to have in the app. The app crashes when it runs out of memory(openglrenderer is out of memory). How do I fix this? Pls help me, Ive spent many a hour on this problem.
Thanks
I would like to create an image gallery that get pictures from a url..this is my code but is not working...
public class ImagesActivity extends Activity
{
private String[] imageIDs= {"http://lh5.ggpht.com/_mrb7w4gF8Ds/TCpetKSqM1I/AAAAAAAAD2c/Qef6Gsqf12Y/s144-c/_DSC4374%20copy.jpg",
"http://lh5.ggpht.com/_Z6tbBnE-swM/TB0CryLkiLI/AAAAAAAAVSo/n6B78hsDUz4/s144-c/_DSC3454.jpg",
"http://lh3.ggpht.com/_GEnSvSHk4iE/TDSfmyCfn0I/AAAAAAAAF8Y/cqmhEoxbwys/s144-c/_MG_3675.jpg",
"http://lh6.ggpht.com/_Nsxc889y6hY/TBp7jfx-cgI/AAAAAAAAHAg/Rr7jX44r2Gc/s144-c/IMGP9775a.jpg"
};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.displayview);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent, View v, int position, long id)
{
// Toast.makeText(getBaseContext(), "pic" + (position + 1) + " selected", Toast.LENGTH_SHORT).show();
//---display the images selected---
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
}
});
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
private int itemBackground;
public ImageAdapter(Context c)
{
context = c;
//---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
//---returns the number of images---
public int getCount() {
return imageIDs.length;
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
ViewsActivity.java
public class ViewsActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
startActivity(new Intent(this, ImagesActivity.class));
}
}
The error is that i cant use imageView.setImageResource(imageIDs[position]); for strings...ant hepl please?
You need to decode the image from the link for example,
URL url = new URL("load your URL");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
img_downloaded.setImageBitmap(bmp);
Update:
I modified your code, you may try with this,
public class MainActivity extends Activity {
private String[] imageIDs = {
"http://lh5.ggpht.com/_mrb7w4gF8Ds/TCpetKSqM1I/AAAAAAAAD2c/Qef6Gsqf12Y/s144-c/_DSC4374%20copy.jpg",
"http://lh5.ggpht.com/_Z6tbBnE-swM/TB0CryLkiLI/AAAAAAAAVSo/n6B78hsDUz4/s144-c/_DSC3454.jpg",
"http://lh3.ggpht.com/_GEnSvSHk4iE/TDSfmyCfn0I/AAAAAAAAF8Y/cqmhEoxbwys/s144-c/_MG_3675.jpg",
"http://lh6.ggpht.com/_Nsxc889y6hY/TBp7jfx-cgI/AAAAAAAAHAg/Rr7jX44r2Gc/s144-c/IMGP9775a.jpg" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
// Toast.makeText(getBaseContext(), "pic" + (position + 1) +
// " selected", Toast.LENGTH_SHORT).show();
// ---display the images selected---
ImageView imageView = (ImageView) findViewById(R.id.imageview1);
URL url = null;
try {
url = new URL(imageIDs[position]);
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeStream(url
.openConnection().getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imageView.setImageBitmap(bmp);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Context c) {
context = c;
// ---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
// ---returns the number of images---
public int getCount() {
return imageIDs.length;
}
// ---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// ---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
// imageView.setImageResource(imageIDs[position]);
URL url;
try {
url = new URL(imageIDs[position]);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
imageView.setImageBitmap(bmp);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
imageView.setBackgroundResource(itemBackground);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return imageView;
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Gallery
android:id="#+id/gallery1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/imageview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/gallery1" />
</RelativeLayout>
In the getView method:
URL newurl = new URL(imageIDs[position]); //Your URL String.
Bitmap image = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
imageView.setImageBitmap(image);
In this Gallery Image using url
Answer Click Link
I have a thumbnail gallery. I would like to impose a bitmap over every thumbnail image. The resultant image should be either a bitmap or a drawable. Hence the gallery now consists of these resultant images. When the user scrolls through the gallery, the resultant image will appear.
I am using the following code
public class GalleryVideo extends Activity {
private Integer[] imgId = {R.drawable.followtheleader,
R.drawable.fruitsalad,
R.drawable.gettingstrong,
R.drawable.gulpgulp,
R.drawable.heythereshakyshaky,
R.drawable.its_a_wiggly_circus,
R.drawable.lettucesing,
R.drawable.monkey_dance,
R.drawable.quack_quack,
R.drawable.vegetable_soup,
R.drawable.walk};
public void onCreate(Bundle savedInstanceState) {
....................
selected_photo = (ImageView)findViewById(R.id.selected_photo);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeFile(photoPath, options);
selected_photo.setImageBitmap(bitmap);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(getApplicationContext()));
th_name = (ImageView)findViewById(R.id.th_name);
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
galleryVideoCounter = position;
Intent myIntent = new Intent(getApplicationContext(), StartDance.class);
startActivity(myIntent);
}
});
....................
}
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 imgId.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
try {
if(position > 0)
{
audioPosCount = position;
}
if(audioPosCount == 0 && audioPosCount != audioPosCount1)
{
galleryAudioCounter++;
audioPosCount1 = audioPosCount;
}
if(audioPosCount > 0 && audioPosCount != -1 && audioPosCount != audioPosCount1)
{
galleryAudioCounter++;
audioPosCount = position;
audioPosCount1 = audioPosCount;
}
return position;
} catch (Exception e) {
}
}
public View getView(int position, View convertView, ViewGroup parent) {
audioPosCount = 0;
ImageView imgView = new ImageView(getApplicationContext());
imgView.setImageResource(imgId[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(((int)(width / 1.45)), ((int)(height / 1.6))));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
return imgView;
}
}
}
I need a sample code for this feature.