How do I populate a gallery and Imageswitcher quickly? - android

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

Related

Android Gallery option for setting wallpaper

This is the project code which is gallery including 4 pictures. I set an option menu which will set picture as a wallpaper. I try to run it works but when i select the "Set as Wallpaper"(menuSet) it is forced to close.
private ImageView foto;
private Gallery gallery;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_naruto_hd_wallpapers);
gallery = (Gallery) findViewById(R.id.gallery);
foto = (ImageView) findViewById(R.id.imageView1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
switch(position) {
case 0: foto.setImageResource(R.drawable.ornek1); break;
case 1: foto.setImageResource(R.drawable.ornek2); break;
case 2: foto.setImageResource(R.drawable.ornek3); break;
case 3: foto.setImageResource(R.drawable.ornek4); break;
}
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.ornek1,
R.drawable.ornek2,
R.drawable.ornek3,
R.drawable.ornek4,
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray attr = mContext.obtainStyledAttributes(R.styleable.GaleriOlusturma);
mGalleryItemBackground = attr.getResourceId(
R.styleable.GaleriOlusturma_android_galleryItemBackground, 0);
attr.recycle();
}
public int getCount() {
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 imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menuSet:
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ornek1);
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(mBitmap);
Toast.makeText(NarutoHdWallpapers.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(NarutoHdWallpapers.this, "Error setting wallpaper", Toast.LENGTH_SHORT).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
If i change these blocks into a simple toast message it works fine.
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ornek1);
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(mBitmap);
Toast.makeText(NarutoHdWallpapers.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(NarutoHdWallpapers.this, "Error setting wallpaper", Toast.LENGTH_SHORT).show();
}
**Note : I put the permission into a manifest

Getting itemid from list<string>

i'm working on wallpaper app
i implemented this code in order to load my pictures from assets
loading-images-from-assets-folder
it's working great but i have issue with getting itemid from the list to use the id to display one of these images in fullscreenactivity or setting wallpaper
public long getItemId(int position) {
return position ;
}
here is my code
public class ImageAdapter extends BaseAdapter{
private Context mContext;
private List<String> list;
private AssetManager assetManager;
public ImageAdapter(Context c, List<String> list) {
mContext = c;
this.list = list;
}
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View view, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
try {
InputStream is = mContext.getAssets().open(list.get(position));
Bitmap bm = BitmapFactory.decodeStream(is);
imageView.setImageBitmap(bm);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(80, 80));
} catch (IOException e) {
e.printStackTrace();
}
// TODO Auto-generated method stub
return imageView;
}
public class Gs3Wallpaper extends Activity {
private ImageButton Gs3Button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.GridView1);
try {
gridView.setAdapter(new ImageAdapter(this ,getImage()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public List<String> getImage()throws IOException {
AssetManager assetManager = getAssets();
String[] files = assetManager.list("gallary");
List<String> list = new ArrayList<String>();
for (String it : files) {
list.add("gallary"+File.separator + it);
}
return list;
i don't know how to get the id from the list i tried to use list.get(position) but it returns string not int so i can't pass that to setImageResource for example
any suggestions?
What you can do is set the image or a path to the image, depending on how you are doing it, in a HashMap in getView() when you set the list item. Use the position for the key and the image as the value
public class MyClass
{
HashMap<String, Bitmap> imageMap = new HashMap<String, Bitmap>();
...
then set it in your getView()
public View getView(int position, View view, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
try {
InputStream is = mContext.getAssets().open(list.get(position));
Bitmap bm = BitmapFactory.decodeStream(is);
imageView.setImageBitmap(bm);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(80, 80));
String posString = String.valueOf(position);
imageMap.put(posString, bm);
} catch (IOException e) {
....
}
Something like this should work for you. Then just use the position to get that bitmap out of the HashMap

Set Wallpaper from gallery according to position

I have a Gallery and an imageView as in this pic: http://i50.tinypic.com/2e6h2so.png
and i need button1 to set the current image as wallpaper
I am having a problem in setResource
myWallpaperManager.setResource(mImageIds[position]);
This is my code:
public class batman extends Activity implements OnClickListener{
Button set;
private int[] mImageIds = {
R.drawable.b,
R.drawable.b1,
R.drawable.b2,
R.drawable.b3,
R.drawable.b4,
R.drawable.b5,
R.drawable.b6,
R.drawable.b7,
R.drawable.b8,
R.drawable.b9,
R.drawable.b10,
R.drawable.b11,
R.drawable.b12,
R.drawable.b13,
R.drawable.b14,
R.drawable.b15,
R.drawable.b16,
R.drawable.b17,
R.drawable.b18,
R.drawable.b19,
R.drawable.b20,
R.drawable.b21,
R.drawable.b22,
R.drawable.b23,
R.drawable.b24,
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.batman);
set=(Button)findViewById(R.id.button1);
set.setOnClickListener(this);
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) {
ImageView imageView =(ImageView)findViewById(R.id.imageView1);
imageView.setImageResource(mImageIds[position]);
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.GalleryA);
mGalleryItemBackground = a.getResourceId(
R.styleable.GalleryA_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
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.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
int id=arg0.getId();
if(id==R.id.button1)
{
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(mImageIds[position]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
First you need this permission in your manifest:
“android.permission.SET_WALLPAPER”
ImageAdapter i = (ImageAdapter)parent.getAdapter();
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),(int)i.getItemId(position));
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(mBitmap);
Toast.makeText(MainActivity.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
}
catch (IOException e)
{
Toast.makeText(MainActivity.this, "Error setting wallpaper", Toast.LENGTH_SHORT).show();
}
}
});
}
First You have to add the SET_WALLPAPER permission to your AndroidManifest.xml
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.SET_WALLPAPER" />
</manifest>
And then change your Button ClickListener as below:
ImageAdapter image = (ImageAdapter)parent.getAdapter();
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),mImageIds[pos]); <----Try by changing this line.
WallpaperManager m_manager = WallpaperManager.getInstance(getApplicationContext());
try {
m_manager.setBitmap(mBitmap);
}
catch (IOException e)
{}
}
});
}

Gallery images from url in android?

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

NullPointerException in Gallery

I am trying make a gallery which contains all images in the assets folder, but I keep getting a NullPointerException error,can anyone tell me what's wrong?
public class MyGallery extends Activity {
private GridView gv;
private SlidingDrawer sd;
private ImageView im;
private int[] icons={R.drawable.download,R.drawable.setwallpaper,
R.drawable.list,R.drawable.other};
private String[] items={"下载到本地","设置为桌面","列表查看","支持本APP"};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
gv = (GridView)findViewById(R.id.grid);
sd = (SlidingDrawer)findViewById(R.id.drawer);
im=(ImageView)findViewById(R.id.openicon);
/* 使用告定义的MyGridViewAdapter设置GridView里面的item内容 */
MyGridViewAdapter adapter=new MyGridViewAdapter(this,items,icons);
gv.setAdapter(adapter);
/* 设定SlidingDrawer被打开的事件处理 */
sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()
{
#Override
public void onDrawerOpened()
{
im.setImageResource(R.drawable.closedrawer);
}
});
/* 设置SlidingDrawer被关闭的事件处理 */
sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener()
{
#Override
public void onDrawerClosed()
{
im.setImageResource(R.drawable.showdrawer);
}
});
Log.d("tag", "抽屉初始化成功");
Gallery g = (Gallery) findViewById(R.id.mygallery);
/*新增几ImageAdapter并设定给Gallery对象*/
g.setAdapter(new ImageAdapter(this,getImage()));
/*设定一个itemclickListener事件*/
g.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent,
View v, int position, long id)
{
}
});
Log.d("tag", "图片初始化成功");
}
private List<String> getImage()
{
/* 设定目前所在路径 */
List<String> it=new ArrayList<String>();
File f=new File("file:///android_asset/");
File[] files=f.listFiles();
Log.d("tag", "读取asset资源");
/* 将所有文件存入ArrayList中 */
for(int i=0;i<files.length;i++)
{
File file=files[i];
if(getImageFile(file.getPath()))
it.add(file.getPath());
}
return it;
}
private boolean getImageFile(String fName)
{
boolean re;
/* 取得扩展名 */
String end=fName.substring(fName.lastIndexOf(".")+1,
fName.length()).toLowerCase();
/* 按扩展名的类型决定MimeType */
if(end.equals("jpg")||end.equals("gif")||end.equals("png")
||end.equals("jpeg")||end.equals("bmp"))
{
re=true;
}
else
{
re=false;
}
return re;
}
/*改写BaseAdapter自定义一ImageAdapter class*/
public class ImageAdapter extends BaseAdapter
{
/*声明变量*/
int mGalleryItemBackground;
private Context mContext;
private List<String> lis;
/*ImageAdapter的构造符*/
public ImageAdapter(Context c,List<String> li)
{
mContext = c;
lis=li;
/* 使用res/values/attrs.xml中的<declare-styleable>定义
* 的Gallery属性.*/
TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
/*取得Gallery属性的Index id*/
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
/*让对象的styleable属性能够反复使用*/
a.recycle();
}
/*几定要重写的方法getCount,传回图片数目*/
public int getCount()
{
return lis.size();
}
/*一定要重写的方法getItem,传回position*/
public Object getItem(int position)
{
return position;
}
/*一定要重写的方法getItemId,传并position*/
public long getItemId(int position)
{
return position;
}
/*几定要重写的方法getView,传并几View对象*/
public View getView(int position, View convertView,
ViewGroup parent)
{
/*产生ImageView对象*/
ImageView i = new ImageView(mContext);
/*设定图片给imageView对象*/
Bitmap bm = BitmapFactory.decodeFile(lis.
get(position).toString());
i.setImageBitmap(bm);
/*重新设定图片的宽高*/
i.setScaleType(ImageView.ScaleType.FIT_XY);
/*重新设定Layout的宽高*/
i.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
/*设定Gallery背景图*/
i.setBackgroundResource(mGalleryItemBackground);
/*传回imageView对象*/
return i;
}
}
}
I am really grateful.
Are you trying to load images from assets folder in your project ??? Well, in that case try reading asset files through AssetManager. I think this might help

Categories

Resources