首页 > 解决方案 > Fresco 从 sdcard 加载图像

问题描述

我正在使用 fresco 库,并且正在尝试将 sd 卡中的图像读取到 imageview 中。

 var CatalogCategories = System.IO.Path.Combine(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).ToString(), "dbsoft");
                SimpleDraweeView draweeView = FindViewById<SimpleDraweeView>(Resource.Id.MainImageView);
                Android.Net.Uri imageUri = Android.Net.Uri.FromFile(new Java.IO.File(CatalogCategories, "Main.jpg"));// For files on device
                draweeView.SetImageURI(imageUri);

我收到错误 Unable t oconvert instance of type androd.widget.imageview to type Com.Facebook.Drawee.View.SimpleDraweeView

如果我将 Imageview 更改为

 <com.facebook.drawee.view.SimpleDraweeView
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:id="@+id/MainImageView" />

然后我收到错误

Android.Views.InflateException:二进制 XML 文件第 1 行:二进制 XML 文件第 1 行:膨胀类 com.facebook.drawee.view.SimpleDraweeView ---> Android.Views.InflateException:二进制 XML 文件第 1 行:错误膨胀类 com.facebook.drawee.view.SimpleDraweeView ---> Java.Lang.Reflect.InvocationTargetException:抛出了“Java.Lang.Reflect.InvocationTargetException”类型的异常。---> Java.Lang.NullPointerException: SimpleDraweeView 未初始化!

标签: androidxamarinfresco

解决方案


该错误已经告诉您需要做什么:通过调用Fresco.initialize(...)您的应用程序来初始化 Fresco,例如

public class MyApplication extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    Fresco.initialize(this);
  }
}

更多信息: http: //frescolib.org/docs/


推荐阅读