我刚刚测试了Dave Morrissey的子采样变焦图像视图:
https://github.com/davemorrissey/subsampling-scale-image-view
如何从Android Sd卡中的特定文件夹(而不是从资源文件夹)加载图像?
以下是所涉及的课程:
公共类ViewPagerActivity扩展了FragmentActivity {
private static final String[] IMAGES = { "ness.jpg", "squirrel.jpg" }; // Open an external folder int the sd card and load my .jpg images into an array dynamically to show on ViewPager.
private ViewPager page;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.view_pager);
getActionBar().setTitle("View pager gallery");
getActionBar().setDisplayHomeAsUpEnabled(true);
PagerAdapter pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
page = (ViewPager)findViewById(R.id.pager);
page.setAdapter(pagerAdapter);
}
@Override
public void onBackPressed() {
if (page.getCurrentItem() == 0) {
super.onBackPressed();
} else {
page.setCurrentItem(page.getCurrentItem() - 1);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
finish();
return true;
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return new ViewPagerFragment(IMAGES[position]);
}
@Override
public int getCount() {
return IMAGES.length;
}
}
}
公共类ViewPagerFragment扩展了Fragment {
private static final String BUNDLE_ASSET = "asset"; // It seems it uses this as the folder name and I would like to change that to another.
private String asset;
public ViewPagerFragment() {
}
public ViewPagerFragment(String asset) {
this.asset = asset;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(layout.view_pager_page, container, false);
if (savedInstanceState != null) {
if (asset == null && savedInstanceState.containsKey(BUNDLE_ASSET)) {
asset = savedInstanceState.getString(BUNDLE_ASSET);
}
}
if (asset != null) {
SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)rootView.findViewById(id.imageView);
imageView.setImageAsset(asset);
}
return rootView;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
View rootView = getView();
if (rootView != null) {
outState.putString(BUNDLE_ASSET, asset);
}
}
}
答案 0 :(得分:0)
使用setImageFile
方法并提供要显示的文件的路径。
imageView.setImageFile("/sdcard/DCIM/DSCM00123.JPG");