通用图像加载器显示来自SD卡的图像

时间:2013-07-07 07:23:16

标签: android universal-image-loader

我的目标是在平滑列表中显示来自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" />

提前谢谢你,

1 个答案:

答案 0 :(得分:1)

首先,我每次通过活动的onCreate时都不会初始化图像加载器。基本上,您不需要多次初始化,因此我建议您在Application onCreate中对其进行初始化。我不知道图像加载器在它已经开始下载一些图像后重新初始化时的行为方式。

除此之外,你所做的事情对我来说似乎有点奇怪 - 你已经拥有了手机上的图像,但你给图像加载器带来了任务,然后再将它们存储起来以便缓存它们。我认为你的配置这个参数是虚假的:.cacheOnDisc()尝试没有它,看看你是否得到更快(删除它将阻止UIL在SD卡上复制)