Search code examples
androidlistviewimageviewxamarin.androidassets

Java.IO.FileNotFoundException while reading assets file from adapter Class - Xamarin


I'm trying to add an image from Assets to imageView. I'm doing this from adapter class. I'm getting Java.IO.FileNotFoundException on an existing file.

Here is my adapter class:

using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.IO;
using Android.Graphics;

namespace test2
{
    public class MyListViewAdapter : BaseAdapter<Person>
    {
        private List<Person> mItems;
        private Context mContext;
        private Button mButton;
        private Activity activity;
    public MyListViewAdapter(Context context, List<Person> items)
    {
        mItems = items;
        mContext = context;
        this.activity = (Activity)context;
    }
    public override int Count
    {
        get
        {
            return mItems.Count;
        }
    }
    public override long GetItemId(int position)
    {
        return position;
    }
    public override Person this[int position]
    {
        get
        {
            return mItems[position];
        }
    }
    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        if (row == null)
        {
            row = LayoutInflater.From(mContext).Inflate(Resource.Layout.listview_row, null, false);
        }

        /*
        some code
        */

        Android.Content.Res.AssetManager assets = mContext.Assets;
        ImageView imageViewStar1 = row.FindViewById<ImageView>(Resource.Id.imageViewStar1);
        var InStr = assets.Open("poi_star_empty.png"); //here is an exception
        Bitmap bitmap = BitmapFactory.DecodeStream(InStr);
        imageViewStar1.SetImageBitmap(bitmap);

        /*
        some code
        */

        return row;
    }


    private class ButtonClickListener : Java.Lang.Object, View.IOnClickListener
    {
        /*
        some code
        */
    }

} }

How can I read this image from non activity class?

That's how I call MyListViewAdapter constructor:

mAdapter = new MyListViewAdapter(this, mItems);

Here is my assets list:

My folders


Solution

  • I made it! For my Asset I had to set build action to AndroidAsset and it works