我尝试将图像从Assets添加到imageView。我是从适配器类做的。我在现有文件上获得Java.IO.FileNotFoundException
。
这是我的适配器类:
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
*/
}
} }
如何从非活动课程中阅读此图片?
我是如何调用MyListViewAdapter构造函数的:
mAdapter = new MyListViewAdapter(this, mItems);
这是我的资产清单:
答案 0 :(得分:1)
我做到了!对于我的资产,我必须将构建操作设置为AndroidAsset并且它可以正常工作
答案 1 :(得分:0)
将文件复制到xamarin.droid中的assets文件夹,然后在文件属性中选择构建操作,然后选择AndroidAsset 然后像
一样访问它 var fd = global::Android.App.Application.Context.Assets.OpenFd(fileName);