0

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

先感谢您,

4

1 回答 1

1

首先,我不会在每次通过onCreate您的活动时初始化图像加载器。基本上,您不需要多个初始化,因此我建议您在Application onCreate. 我不知道如果在它已经开始下载一些图像之后重新初始化它,图像加载器会如何表现。

除此之外,你所做的事情对我来说似乎有点奇怪——你的手机上已经有了图像,但是你给图像加载器一个任务来获取它们并再次存储它们,以便将它们缓存起来。我认为你的配置的这个参数是虚假的:.cacheOnDisc()尝试不使用它,看看你是否更快(删除它会阻止 UIL 在 SD 卡上重复)

于 2013-07-07T07:44:26.533 回答