Search code examples
androidgridviewuniversal-image-loader

Unable to load images for GridView from drawable using Universal Image Loader


I'm calling the UIL's displayImage method but it is not accepting the image URI. Here is my code for getView. I'm getting the error:

UnsupportedOperationException: UIL doesn't support scheme(protocol) by default [2130837504]. You should implement this support yourself (BaseImageDownloader.getStreamFromOtherSource(...))

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;

        if (convertView == null) {

            convertView = mLayoutInflater.inflate(R.layout.grid_view_card_layout, parent, false);
            holder = new ViewHolder();
            holder.participantName = (TextView) convertView.findViewById(R.id.grid_model_name);
            holder.participantCountry = (TextView) convertView.findViewById(R.id.grid_country_name);
            holder.participantImage = (ImageView) convertView.findViewById(R.id.grid_model_image);
            holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progress);
            convertView.setTag(holder);

        } else {
            holder=(ViewHolder)convertView.getTag();
        }

        applyTypeFace(convertView);

        holder.participantName.setText(ParticipantName[position]);
        holder.participantCountry.setText(ParticipantCountry[position]);
        //holder.participantImage.setImageResource(ImageId[position]);

        //Here lies the problem
        ImageLoader.getInstance()
                .displayImage(String.valueOf(ImageId[position]), holder.participantImage);

        return convertView;
    }

Any help, please?


Solution

  • You have to use "drawable://" + id of the drawable resource

    Example:

    ImageLoader.getInstance().displayImage("drawable://" + R.drawable.icon_teste,holder.participantImage);
    

    If you're trying to get the drawable by it's name instead of the id you can get it's identifier using this:

    int id = getContext().getResources().getIdentifier(ICON_NAME, "drawable", getContext().getPackageName());