使用通用图像加载器
https://github.com/nostra13/Android-Universal-Image-Loader.git
将 Image url 作为您的 sd 卡图像路径。
/**
*adapter class for view pager
*
*/
private class ImagePagerAdapter extends PagerAdapter {
LayoutInflater inflater;
public ImagePagerAdapter() {
inflater=getActivity().getLayoutInflater();
}
@Override
public int getCount() {
return viewPagerList.size();
}
@Override
public void finishUpdate(View container) {
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View imageLayout = inflater.inflate(R.layout.item_pager_image, container, false);
ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
String loadURL=null;
if(connectivityManager.hasDataConnectivity()){
loadURL=viewPagerList.get(position).getModelImageUrl();
}
else {
String fileName=viewPagerList.get(position).getModelImageUrl();
fileName = fileName.replace(':', '/');
fileName = fileName.replace('/', '_');
loadURL="file://"+Environment.getExternalStorageDirectory()+"/"+folder+"/"+fileName;
}
BaseActivity.imageLoader.displayImage(loadURL, imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
spinner.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
String message = null;
switch (failReason) {
case IO_ERROR:
message = "Input/Output error";
break;
case OUT_OF_MEMORY:
message = "Out Of Memory error";
break;
case NETWORK_DENIED:
message = "Downloads are denied";
break;
case UNSUPPORTED_URI_SCHEME:
message = "Unsupported URI scheme";
break;
case UNKNOWN:
message = "Unknown error";
break;
}
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(final String imageUri, View view, final Bitmap loadedImage) {
spinner.setVisibility(View.GONE);
Logger.show(Log.INFO, "@@@@@@@", imageUri);
String uniqueUrlName = imageUri.replace(':', '/');
uniqueUrlName = uniqueUrlName.replace('/', '_');
File file = new File(Environment.getExternalStorageDirectory()
.toString()
+ "/"
+ folder
+ "/"
+ uniqueUrlName);
if(!file.exists()){
new Thread(new Runnable() {
public void run() {
String imageUrlString="file://"+GetModels.getModelURL(imageUri,folder,loadedImage,context);
Logger.show(Log.INFO, context.getClass().getName(), "image loaded my folfer"+ imageUrlString);
}
}).start();
}
Logger.show(Log.INFO, context.getClass().getName(), "image loaded loader "+ StorageUtils.getCacheDirectory(context));
}
});
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Bundle b=new Bundle();
b.putString("ProductName", viewPagerList.get(pos).getModelName());
b.putString("ProductPrice", viewPagerList.get(pos).getModelPrice());
b.putString("ProductUrl",viewPagerList.get(pos).getModelLink() );
String loadURL=null;
if(connectivityManager.hasDataConnectivity()){
loadURL=viewPagerList.get(pos).getModelImageUrl();
}
else {
String fileName=viewPagerList.get(pos).getModelImageUrl();
fileName = fileName.replace(':', '/');
fileName = fileName.replace('/', '_');
loadURL="file://"+Environment.getExternalStorageDirectory()+"/"
+folder+"/"+fileName;
}
b.putString("ProductImage", loadURL);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left,R.anim.slide_in_left, R.anim.slide_out_right);
ProductOverview commentFragment = new ProductOverview();
commentFragment.setArguments(b);
fragmentTransaction.replace(R.id.product_container, commentFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
((ViewPager) container).addView(imageLayout, 0);
return imageLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View container) {
}
}