【问题标题】:Using volley and Glide to download, display and cache images使用 volley 和 Glide 下载、显示和缓存图像
【发布时间】:2017-02-06 15:25:02
【问题描述】:

我无法理解这一点,可能是因为我缺乏经验。我在服务器上有图像,我在 JSON 中获取它们的路径以及描述和名称。我正在使用 Volley 获取所有这些数据。

private static final String TAG = "StoreActivity";
private RecyclerView shopsRecyclerView;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<Store> stores;
private StoreAdapter storeAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_store);

    shopsRecyclerView = (RecyclerView) findViewById(R.id.shopsRecyclerView);
    shopsRecyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    shopsRecyclerView.setLayoutManager(layoutManager);
    stores = new ArrayList<>();
    storeAdapter = new StoreAdapter(getApplicationContext(), stores);
    shopsRecyclerView.addOnItemTouchListener(new StoreAdapter.RecyclerTouchListener(getApplicationContext(), shopsRecyclerView, new StoreAdapter.ClickListener() {
        @Override
        public void onClick(View view, int position) {
            Intent i = new Intent(StoreActivity.this, ProductsActivity.class);
            startActivity(i);
        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));
    fetchStores();
}

private void fetchStores() {
    JsonObjectRequest fetchAllStores = new JsonObjectRequest(Request.Method.POST, API.GET_STORES, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG, "Fetch Stores: " + response);
            showStores(response);
            shopsRecyclerView.setAdapter(storeAdapter);
            storeAdapter.notifyDataSetChanged();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG, "Fetch Stores Error: " + error.getMessage());
        }
    });
    ApplicationController.getInstance().addToRequestQueue(fetchAllStores);
}

private void showStores(JSONObject response) {
    try {
        JSONArray jsonArray = response.getJSONArray("images");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Store store = new Store();
            store.setId(jsonObject.getString("id"));
            store.setImage_url(jsonObject.getString("url"));
            store.setTitle(jsonObject.getString("name"));
            stores.add(store);
        }
    } catch (JSONException e) {
        Log.d(TAG, "Show Stores: " + e.getMessage());
    }
}

然后我把从 Volley 获得的路径/url 放到 Glide 中,并在 RecyclerView 中加载所有图像。

@Override
public void onBindViewHolder(StoreViewHolder holder, final int position) {
    Store store = stores.get(position);
    holder.tvDescription.setText(store.getTitle());
    Glide.with(context)
            .load(store.getImage_url())
            .placeholder(android.R.drawable.ic_menu_upload_you_tube)
            .error(android.R.drawable.stat_notify_error)
            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .into(holder.ivImage);

}

下次我打开同一个activity,请求又被发送,图像又被下载了?或者它们以前是否以某种方式缓存过并且是从缓存中加载的? 当我关闭互联网并打开相同的活动时,什么也没有发生,因为无法发送凌空请求,并且我认为缓存的图像没有显示。 解决我的问题的最佳方法是什么?基本上,我需要从服务器获取图像的路径并将它们放在我的 recyclerview 中,但我也希望它们被缓存,这样它们就不会每次都被下载。

【问题讨论】:

    标签: android caching android-volley android-glide


    【解决方案1】:

    我想建议你使用 Glide lib,非常简单并且为你的应用缓存图像,我自己尝试过。您还可以在 url 中添加签名,以便下次更新时必须更改。

    Glide Url

    以下是代码片段:

    dependencies {
            // glide
            compile 'com.github.bumptech.glide:glide:3.7.0'
      }
    
    String imgUrl = "http://imageurl";
    
    ImageView imageView = (ImageView) view.findViewById(R.id.thumbnail);
    
    
        Glide.with(mContext).load(imgUrl)
                    .thumbnail(0.5f)
                    .crossFade()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(imageView);
    

    希望对您有所帮助。

    【讨论】:

    • 嘿,我已经使用了 Glide,但我必须以某种方式从服务器获取图像,这就是我使用 volley 的原因。其余的在我的问题中。 :)
    • 您要获取图片吗?如果是,那么您必须将图像保存到服务器,然后您必须获取图像的链接。你可以使用 Retrofit 而不是 volley。
    • Darpan,我已经完成了所有这些。我想知道的是,通过 volley 获取然后在带有 Glide 的图像视图中显示的图像是否被缓存?下次我运行应用程序时,Glide 会加载缓存的图像还是等待 volley 再次获取它们?我也想知道这方面的最佳做法是什么。
    • @jlively 我的朋友我告诉过你 Glide 会缓存图像。 Glide 使用各种磁盘缓存策略,您可以根据需要使用//这是各种类型。 DiskCacheStrategy.NONE 不缓存任何内容 DiskCacheStrategy.SOURCE 仅缓存原始全分辨率图像。 DiskCacheStrategy.RESULT 只缓存最终图像,降低分辨率后。 DiskCacheStrategy.ALL 缓存图像的所有版本是的,您的图像是使用 Glide 缓存的。这就是我所知道的,兄弟。希望它有帮助:)
    猜你喜欢
    • 2015-09-13
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多