Gallery显示,读取SD卡内的图片
在图片当道的如下,还是有图更有说服力。 小子不才,所以这个demo是我东找西找然后自己凑的一个例子。内有失当之处,多包涵多指正! 等了三天才能发帖子,这点JAVAEYE真的应该改变一下了,要不然把我这样的孩子憋生啥样了。。。 1。首先定义xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#55000000"
<ImageSwitcher //此控件与Gallery会经常用的,切记 android:id="@+id/switcher" android:layout_width="wrap_content" android:layout_height="350dip" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" />
import java.io.File; import java.util.ArrayList; import java.util.List;
import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.view.View.OnClickListener; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.Toast; import android.widget.ViewSwitcher; import android.widget.AdapterView.OnItemClickListener; import android.widget.Gallery.LayoutParams;
public class BgPictureShowActivity extends Activity implements AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
private List<String> ImageList;
private String list;
private ImageSwitcher mSwitcher;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// //去掉状态栏,且显示自己程序名称 // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, // WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.pictureshow);
ImageList=getSD();
list = ImageList.toArray(new String);
/*设定Switcher*/
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
/*设定载入Switcher的模式*/
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
/*设定输出Switcher的模式*/
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
mSwitcher.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Toast.makeText(BgPictureShowActivity.this, "点击了", Toast.LENGTH_SHORT).show();
}
});
Gallery g = (Gallery) findViewById(R.id.mygallery);
/*新增几ImageAdapter并设定给Gallery对象*/
g.setAdapter(new ImageAdapter(this,getSD()));
g.setOnItemSelectedListener(this);
/*设定一个itemclickListener事件*/
g.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent,
View v, int position, long id)
{
Toast.makeText(BgPictureShowActivity.this, "dianjiale ", Toast.LENGTH_SHORT).show();
}
});
}
private List<String> getSD()
{
/* 设定目前所在路径 */
List<String> it=new ArrayList<String>();
File f=new File("/sdcard/");
File files=f.listFiles();
/* 将所有文件存入ArrayList中 */
for(int i=0;i<files.length;i++)
{
File file=files*;
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 Gallery.LayoutParams(136, 88));
/*设定Gallery背景图*/
i.setBackgroundResource(mGalleryItemBackground);
/*传回imageView对象*/
return i;
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
String photoURL=list;
Log.i("A",String.valueOf(position));
mSwitcher.setImageURI(Uri.parse(photoURL));
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
return i;
}
} 这段代码很长,看的时候肯定会头疼加DAN疼,但是一定要挺住啊,MARS大神一再告诫咱们,这个行业的特点就是要有耐心。 :oops: 3。对了,还有一个非常重要的事情,就是在values文件夹下面,新建一个xml,内书:
楼主,问一下,我这里怎么报异常呢,是 OutOfMemoryErroy的异常,为什么呢,我的号是 1297234270@qq.com 帮忙一下吧
显示出来了,只是SDCARD根目录下的图片,谢谢了!!!
为什么我显示不出来任何图片啊
谢谢分享,已经转载到 http://developer.aiwgame.com/gallery-show-an-image-from-android-sd-card.html