-1

I have 700 images in Drawable and i have a list of image name and i need to load it into imageview programily
Image is in webp formate and less than 100kb
images in recycler view

where is wrong

Java Code

Drawable drawable = mContext.getResources().getDrawable(mContext.getResources().getIdentifier(mData.get(position).getImage(), "drawable", mContext.getPackageName()));             
    holder.image.setImageDrawable(drawable);

Layout

<ImageView
                android:id="@+id/fish_image"
                android:layout_width="match_parent"
                android:scaleType="fitXY"
                android:minWidth="100dp"
                android:minHeight="100dp"
                android:background="#ffffff"
                android:layout_height="wrap_content"
                />


I have 700 images in Drawable and I have a list of image name and I need to load it into image view programmatically is there any other solution

Update it worked
i moved image to Assets folder and load it from there

try {
            // get input stream
            InputStream ims = mContext.getAssets().open(mData.get(position).getImagename()+".webp");
            // load image as Drawable
            Drawable d = Drawable.createFromStream(ims, null);
            // set image to ImageView
            holder.image.setImageDrawable(d);
            ims .close();
        }
        catch(IOException ex)
        {
            Log.e("image_io",ex.getMessage()+"");
           // return;
        }
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
Midhilaj
  • 4,905
  • 9
  • 45
  • 88
  • Please share imageview height and width – Sushant Somani Aug 12 '18 at 07:30
  • i have 700 images in Drawable and i have a list of image name and i need to load it into imageview programily – Midhilaj Aug 12 '18 at 07:33
  • How to use setImageResource() ? – Midhilaj Aug 12 '18 at 07:34
  • What is `getIdentifier()` returning there? What is `getImage()` returning? Do you actually have a drawable resource by that name, applicable to the current configuration? What happens if you temporarily use the `R.drawable` value? For that matter, why aren't you just using the `R.drawable` values in your data model to begin with? – Mike M. Aug 12 '18 at 07:37
  • getImage() return string example "Malawi_shell_dweller" – Midhilaj Aug 12 '18 at 07:38

2 Answers2

1

You should use the name of the drawable as string and not getImageuri():

Drawable drawable = mContext.getResources().getDrawable(mContext.getResources().getIdentifier("drawable_name", "drawable", mContext.getPackageName()));
0
Uri uri_for_drawable = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
                "://" + getResources().getResourcePackageName(R.drawable.your_image_name)
                + '/' + getResources().getResourceTypeName(R.drawable.your_image_name) + '/' + getResources().getResourceEntryName(R.drawable.your_image_name) );

Use the above code to get the image uri from drawable folder.

Gourango Sutradhar
  • 1,461
  • 10
  • 16