使用Data绑定将图像URL设置为NetworkImageView会产生FileNotFoundException

时间:2017-06-30 10:21:55

标签: android android-recyclerview android-volley android-databinding networkimageview

我正在使用Volley Library从服务器获取图像并使用RecyclerView中的NetworkImageView显示它。当我使用Android数据绑定时,我会遇到异常。

Unable to decode stream: java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
resolveUri failed on bad bitmap uri

使用实用方法(从适配器本身调用setImageUrl),否则我能够显示图像。

  @BindingAdapter("imgurl")
   public void setImageUrl(NetworkImageView view, String imageURL) {
    Context context = view.getContext();
    ImageLoader mImageLoader = CustomVolleyRequestQueue.getInstance(context).getImageLoader();
    try {
        ImageLoader.getImageListener(view, R.drawable.story_big_default, R.drawable.story_big_default);
        Log.d("STORYMODEL", "setImageUrl: "+imageURL.trim());
        view.setImageUrl(imageURL.trim(), mImageLoader);
    } catch (Exception e) {
        e.printStackTrace();
        view.setImageResource(R.drawable.story_big_default);
    }
}

   <com.android.volley.toolbox.NetworkImageView
    android:id="@+id/ivBackground"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@drawable/story_small_default"
    android:scaleType="fitXY"
    android:src="@{storymodel.imgurl}" />

请帮助,我在这里可能缺少什么?

5 个答案:

答案 0 :(得分:0)

它可能是imageURL.trim()网址可​​能有黑色空格使用replace(" ","%20")

答案 1 :(得分:0)

我通过更改BindingAdapter参数来解决了这个问题。这与Model和Imageview中的dataitem之间的映射有关。 https://mobikul.com/data-binding-part-ii-image-binding/

答案 2 :(得分:0)

# remote_app.tasks.py
def add(*args):
    return sum(args)
    # simulating some processing delay. 
    # Removing this does not fix the problem
    time.sleep(random.random())

# test_command.py
l1 = [
    [2, 2],
    [3, 3],
]
l2 = [
    [4, 4],
    [6, 6],
]

def get_remote_chain(i):
    """ 
    Call the tasks remotely. This seems to work fine whith a small number 
    of calls, but ends up hanging when i start to call it a bunch.  
    Behaviour is basically the same as described above.
    """
    t1 = remote_app.signature(
        'remote.path.to.add',
        args=l1[i],
        queue='somequeue'
    )
    t2 = remote_app.signature(
        'remote.path.to.add',
        args=l2[i],
        queue='somequeue'
    )

    return celery.chain(
        t1, t2, app=simi_app
    )


def get_local_chain(i):
    """
    I copy pasted the task locally, and tried to call this version of the 
    chain instead:
    Everything works fine, no matter how many times we call it
    """
    return celery.chain(
        add.s(*l1[i]),
        add.s(*l1[i]),
    )


def launch_chains():

    results = OrderedDict()
    for i in range(100):
        for j in range(2):

            # task_chain = get_local_chain(j)
            task_chain = get_remote_chain(j)

            results['%d-%d' % (j, i)] = task_chain.apply_async()

    return results

class Command(BaseCommand):


    def handle(self, *args, **kwargs):
        results = launch_chains()
        print('dict ok')

        for rid, r in results.items():
            print(rid, r.get())

        # Trying an alternate way to process the results, trying to 
        # ensure we wait until each task is ready.
        # Again, this works fine with local tasks, but ends up hanging, 
        # apparently randomly, after a few remote ones have been "processed".
        #
        # rlen = len(results)
        # while results:
        #     for rid, r in results.items():
        #         if r.ready():
        #             print(rid, r.status)
        #             print(r.get())
        #             results.pop(rid)
        #     if len(results) != rlen:
        #         print('RETRYING ALL, %d remaining' % len(results))
        #         rlen = len(results)

在您的图片视图中创建一个新属性&#39; imageUrl&#39;在名称空间&#39; app&#39;。在你的数据绑定助手类或pojo中创建一个如下方法和一个String值&#39; brandLogo&#39;。使用图像的url初始化此变量的值。

<com.android.volley.toolbox.NetworkImageView
        android:id="@+id/campaign_logo"
        android:layout_width="80dp"
        android:layout_height="80dp"           
        android:adjustViewBounds="true"
        android:background="@drawable/border_rectangle"
        android:padding="2dp"
        app:imageUrl="@{helper.brandLogo}" />

**如果您使用过Volley,您必须了解使用ImageLoader从网络加载图像,或者您可以使用Picasso或其他库加载相同的方法

此外,您还可以添加占位符,按照上述相同步骤创建新属性并将绑定适配器注释编辑为

@BindingAdapter("imageUrl")
public static void getBrandLogo(View view, String imageUrl) {
    NetworkImageView imageView = (NetworkImageView) view;
    imageView.setImageUrl(imageUrl, imageLoader);
}

答案 3 :(得分:0)

 <de.hdodenhof.circleimageview.CircleImageView
                    android:layout_width="@dimen/margin_80"
                    android:layout_height="@dimen/margin_80"
                    android:background="@drawable/ic_user_placeholder"
                    app:srcProfilePhoto="@{profileData.profilePhoto}" // pass url
                    app:civ_border_color="@color/gray"
                     />

最后在java文件中使用下面的代码

@BindingAdapter("app:srcProfilePhoto")
    public static void srcProfilePhoto(ImageView view, String imageUrl) {
        if (!TextUtils.isEmpty(imageUrl)) {
            RequestOptions options = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.drawable.ic_user_placeholder);
            Glide.with(view.getContext())
                    .load(imageUrl)
                    .apply(options)
                    .into(view);
        }
    }

答案 4 :(得分:-3)

使用Picasso库在ImageView替换NetworkImageView

中加载图像

例如,

ImageView thumbNail;

在Imageview中加载图片,

Picasso.with(activity).load(url).noFade().into(thumbNail);