如何显示存储在SD卡中的所有图像?

时间:2011-11-24 13:41:44

标签: android android-sdcard

我想根据存储的文件夹显示存储在SD卡中的所有图像。如果文件夹中有2个图像,则它应该只显示两个图像。假设SD卡包含文件夹名称影片并且还有一些图像,那么当我单击影片文件夹时,它应该显示所有五个图像。我使用左侧的列表视图来显示存储图像的所有SD卡文件夹。

我的代码是:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private String Path;
    String [] imagesFile;
    File dirName;
    Bitmap bm[];
    int count;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public ImageAdapter(Context c, String nAndroids) {
        mContext = c;
        Path=nAndroids;
        dirName=new File(Path+"/");
        if(dirName.isDirectory()){
            imagesFile=dirName.list();
            count=imagesFile.length ;

        }
    }

    public int getCount() {
        if(imagesFile!=null){
            return count;
        }
         return 0;

    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView=null;
        System.gc();
        if( imagesFile!=null  && imagesFile.length>0)
        {
            for(String bitmapFileName : imagesFile)
            {
                if (convertView == null) {  // if it's not recycled, initialize some attributes
                    imageView = new ImageView(mContext);
                    imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    imageView.setPadding(8, 8, 8, 8);

                    BitmapFactory.Options options = new BitmapFactory.Options();  
                    options.inSampleSize = 2;
                    Bitmap bmp = BitmapFactory.decodeFile(dirName.getPath() + "/" + bitmapFileName,options);
                    Log.e("Images is Decoded in ImageAdapter Class","<<-->>"+bmp);
                    imageView.setImageBitmap(bmp);
                } else {
                    imageView = (ImageView) convertView;
                }
        }


        }

        else 
        {
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);

            } else {
                imageView = (ImageView) convertView;
            }
        }
   return imageView;
    }

0 个答案:

没有答案