2016-06-29 66 views
0

我試圖從資產添加圖像到imageView。我從適配器類來做這件事。我正在獲取現有文件上的Java.IO.FileNotFoundException。Java.IO.FileNotFoundException從適配器類讀取資產文件 - Xamarin

這裏是我的適配器類:

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); 

這裏是我的資產清單:

My folders

回答

0

我成功了!對於我的資產,我不得不設置生成操作AndroidAsset和它的作品

0

將文件複製到文件夾資產在xamarin.droid,然後在文件選擇生成操作的屬性,然後AndroidAsset 然後訪問它像

var fd = global::Android.App.Application.Context.Assets.OpenFd(fileName);