我的目标是在平滑列表中显示来自 SD 卡的图像(缩略图),所以我稍微编辑了演示样本(从 web 加载图像到 gridview)以从 SD 卡加载图像以查看性能,以及图像未缓存时(第一次)它很慢,因为它应该从本地 SD 卡而不是网络加载图像,但是在图像被缓存后,我对结果非常满意,请看看,如果我做错了什么,请告诉我,或者我如何在第一次加载时提高性能,我也对其他可能更专门从 SD 卡加载图像的库开放,这里是代码:-
public class ImageGridActivity extends AbsListViewBaseActivity {
// get files path in camera folder
public String[] getimages() {
String[] images=new String[175];
String path="file:///mnt/sdcard/DCIM/Camera/";
int counter=0;
String name;
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "dcim/camera");
for (File f : yourDir.listFiles()) {
if (f.isFile()){
name = path + f.getName().toString() ;
images[counter]=name;
// make something with the name
counter++;}
}
return images;
}
String[] imageUrls= getimages();
DisplayImageOptions options;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_image_grid);
Bundle bundle = getIntent().getExtras();
// imageUrls = bundle.getStringArray(Extra.IMAGES);
// imageUrls[0]="file:///mnt/sdcard/DCIM/Camera/abc.jpg";
options = new DisplayImageOptions.Builder()
/* .showStubImage(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisc(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build(); */
.showStubImage(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.showImageOnFail(R.drawable.ic_launcher).cacheInMemory()
.cacheOnDisc().bitmapConfig(Bitmap.Config.RGB_565).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext()).discCacheExtraOptions(720 , 1280 , CompressFormat.PNG, 0, null).build();
ImageLoader.getInstance().init(config);
listView = (GridView) findViewById(R.id.gridview);
((GridView) listView).setAdapter(new ImageAdapter());
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startImagePagerActivity(position);
}
});
}
private void startImagePagerActivity(int position) {
Intent intent = new Intent(this, ImagePagerActivity.class);
intent.putExtra(Extra.IMAGES, imageUrls);
intent.putExtra(Extra.IMAGE_POSITION, position);
startActivity(intent);
}
public class ImageAdapter extends BaseAdapter {
@Override
public int getCount() {
return imageUrls.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) {
imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
} else {
imageView = (ImageView) convertView;
}
imageLoader.displayImage(imageUrls[position], imageView, options);
return imageView;
}
}
}
item_grid_image
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="120dip"
android:adjustViewBounds="true"
android:contentDescription="@string/descr_image"
android:scaleType="centerCrop" />
先感谢您,