嗨有一个活动,它从可绘制文件夹中读取图像并在 viewpager 中显示它们。下面是我的代码。现在我想将 SD 卡中的图像显示到 viewpager。应该在下面的代码中进行哪些更改以从 SD 卡中读取图像并在 viewpager 中显示它们。
public class TestClass extends Activity {
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.testclass);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
ViewPager myPager = (ViewPager) findViewById(R.id.viewpager_testclass);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
private int imageArra[] = {
R.drawable.a01,
R.drawable.a02,
R.drawable.a03
};
这是 PagerAdapter
public class ViewPagerAdapter extends PagerAdapter {
Activity activity;
int imageArray[];
public ViewPagerAdapter(Activity act, int[] imgArra) {
imageArray = imgArra;
activity = act;
}
public int getCount() {
return imageArray.length;
}
public Object instantiateItem(View collection, int position) {
ImageView view = new ImageView(activity);
view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
view.setScaleType(ScaleType.FIT_XY);
view.setBackgroundResource(imageArray[position]);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}