我要提取sdcard 里面的图片,可是这样写出来没有图片显示。模拟器没有sd卡所以我在当前目录建了个文件夹(名为:sdcard)
我是初学者,请大家帮帮忙,谢谢。public class MainActivity extends Activity {
private TextView mTextView01;
private Gallery mGallery01;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView01 = (TextView) findViewById(R.id.myTextView);
mTextView01.setText(getString(R.string.hello));
mGallery01=(Gallery) findViewById(R.id.myGallery);
mGallery01.setAdapter(new ImageAdapter(this,getSD())); }
private List<String> getSD() {
List<String> list = new ArrayList<String>();
File f = new File("\\sdcard");
File[] files = f.listFiles();
for(int i=0; i<files.length; i++){
File file = files[i];
if(getImageFile(file.getPath()))
list.add(file.getPath());
}
return list;
}
private boolean getImageFile(String fName) {
boolean re;
String end = fName.substring(fName.lastIndexOf(".")+1,fName.length()).toLowerCase();
if(end.equals("jpg") || end.equals("png") || end.equals("bmp") || end.equals("gif"))
{
re = true;
}else {
re = false;
}
return re;
}
public class ImageAdapter extends BaseAdapter
{ /* 类成员 myContext为Context父类 */
private Context myContext;
/*声明变量*/
int mGalleryItemBackground;
private List<String> list;
/* 建构子类只有一个参数,即要储存的Context */
public ImageAdapter(Context c,List<String> li)
{/* 引用成员变量myContext */
this.myContext = c;
list = 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();
}
/* 回传所有已定义的图片总数量 */
public int getCount()
{
return list.size();
}
/* 利用getItem方法,取得目前容器中影像的数组ID */
public Object getItem(int position)
{
return position;
} public long getItemId(int position)
{
return position;
} /*显示Gallery里的模式*/
public View getView(int position, View convertView,ViewGroup parent)
{
ImageView i = new ImageView(this.myContext);
Bitmap bm = BitmapFactory.decodeFile(list.get(position).toString());
i.setImageBitmap(bm);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(136,88));
/*设定Gallery背景图*/
i.setBackgroundResource(mGalleryItemBackground);
/*传回imageView对象*/
return i;
}
}
}
我是初学者,请大家帮帮忙,谢谢。public class MainActivity extends Activity {
private TextView mTextView01;
private Gallery mGallery01;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView01 = (TextView) findViewById(R.id.myTextView);
mTextView01.setText(getString(R.string.hello));
mGallery01=(Gallery) findViewById(R.id.myGallery);
mGallery01.setAdapter(new ImageAdapter(this,getSD())); }
private List<String> getSD() {
List<String> list = new ArrayList<String>();
File f = new File("\\sdcard");
File[] files = f.listFiles();
for(int i=0; i<files.length; i++){
File file = files[i];
if(getImageFile(file.getPath()))
list.add(file.getPath());
}
return list;
}
private boolean getImageFile(String fName) {
boolean re;
String end = fName.substring(fName.lastIndexOf(".")+1,fName.length()).toLowerCase();
if(end.equals("jpg") || end.equals("png") || end.equals("bmp") || end.equals("gif"))
{
re = true;
}else {
re = false;
}
return re;
}
public class ImageAdapter extends BaseAdapter
{ /* 类成员 myContext为Context父类 */
private Context myContext;
/*声明变量*/
int mGalleryItemBackground;
private List<String> list;
/* 建构子类只有一个参数,即要储存的Context */
public ImageAdapter(Context c,List<String> li)
{/* 引用成员变量myContext */
this.myContext = c;
list = 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();
}
/* 回传所有已定义的图片总数量 */
public int getCount()
{
return list.size();
}
/* 利用getItem方法,取得目前容器中影像的数组ID */
public Object getItem(int position)
{
return position;
} public long getItemId(int position)
{
return position;
} /*显示Gallery里的模式*/
public View getView(int position, View convertView,ViewGroup parent)
{
ImageView i = new ImageView(this.myContext);
Bitmap bm = BitmapFactory.decodeFile(list.get(position).toString());
i.setImageBitmap(bm);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(136,88));
/*设定Gallery背景图*/
i.setBackgroundResource(mGalleryItemBackground);
/*传回imageView对象*/
return i;
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货