如何在文件目录中的listview中显示真实图像

时间:2013-04-17 06:38:10

标签: android

如何在文件列表中显示图片?这是我的代码,显示图标只有我想显示真实的图像而不是图标我该怎么办?请帮我看看如何在gridview中显示真实图像请帮帮我

      private void inflateListView(File[] files){
    List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();
    for(int i=0;i<files.length;i++){
        Map<String, Object> listItem=new HashMap<String, Object>();

        if(files[i].isDirectory()){

            listItem.put("icon", R.drawable.folder);

        }
        else
        {
            listItem.put("icon", R.drawable.file);
        }
        listItem.put("fileName", files[i].getName());
        listItems.add(listItem);
    }
    SimpleAdapter simpleAdapter=new SimpleAdapter(this,listItems,R.layout.line,new  
 String[]{"icon","fileName"},new int[]{R.id.icon,R.id.file_name});
    gridView.setAdapter(simpleAdapter);

}

1 个答案:

答案 0 :(得分:0)

获取文件的路径将其存储在arraylist中。有自定义适配器。使用路径在grdiview中显示图像。

我建议你使用通用图像加载器

说明:

  1. 获取文件夹下的文件路径。 (假设文件夹只有图像。)。将其存储在ArrayList中。将arraylist和活动上下文传递给适配器construtor

  2. 让CustomAdapter说MyAdapter扩展BaseAdapter。为每行充气自定义布局,并根据位置显示颜色。

  3. 使用下面的通用图像加载器

  4. https://github.com/nostra13/Android-Universal-Image-Loader

    ArrayList<String> f= new ArrayList<String>
    private File[] listFile; 
    
    public void getFromSdcard()
    {
        File file=  new File(android.os.Environment.getExternalStorageDirectory(),"TTImages_cache");
    
            if (file.isDirectory())
            {
                listFile = file.listFiles();
    
                for (int i = 0; i < listFile.length; i++)
                {
    
                    f.add(listFile[i].getAbsolutePath());
    
                }
            }
    }
    
    
        adapter=new LazyAdapter(Activity.this,thumb);
    gridView.setAdapter(adapter);
    

    在CustomAdapter中

    private Activity activity;
    private ArrayList<String> data = new ArrayList<String>();
    private LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    DisplayImageOptions options;
    
    public LazyAdapter(Activity a, ArrayList<String> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        File cacheDir = StorageUtils.getOwnCacheDirectory(a, "MyRaghu");
    
    
     // Get singletone instance of ImageLoader
       imageLoader = ImageLoader.getInstance();
     // Create configuration for ImageLoader (all options are optional)
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
                  // You can pass your own memory cache implementation
                 .discCacheExtraOptions(1024, 1024, CompressFormat.PNG, 100)
                 .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
                 .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
                 .enableLogging()
                 .build();
     // Initialize ImageLoader with created configuration. Do it once.
     imageLoader.init(config);
        //imageLoader.init(ImageLoaderConfiguration.createDefault(a));
       // imageLoader=new ImageLoader(activity.getApplicationContext());
        options = new DisplayImageOptions.Builder()
        .showStubImage(R.drawable.ic_launcher)
        .cacheInMemory()
        .cacheOnDisc()
        .displayer(new RoundedBitmapDisplayer(20))
        .build();
      }
    

    在你的getView

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
         vi = inflater.inflate(R.layout.rowimage, null);
    
        ImageView image=(ImageView)vi.findViewById(R.id.ivv); 
        ProgressBar pb= (ProgressBar)vi.findViewById(R.id.pb); 
        //imageLoader.displayImage(data.get(position).toString(), image,options);
    
        return vi;
    }