Volley NetworkImageView

时间:2016-02-13 12:48:44

标签: android gridview android-volley

我有一个GridView,其中的图像是由Volley Library从网络中提取的。 当我点击一个项目后,图像会在下载后以全屏分辨率全屏显示,这需要一些时间。 所以我用AppBar改变了它,但是我仍然使用相同的方法来显示它,这也需要相同的时间。

是:

private void fetchFullResolutionImage() {
    String url = selectedPhoto.getPhotoJson();

    // volley's json obj request
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url,
            (String)null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG,
                    "Image full resolution json: "
                            + response.toString());
            try {
                // Parsing the json response
                JSONObject entry = response
                        .getJSONObject(TAG_ENTRY);

                JSONArray mediacontentArry = entry.getJSONObject(
                        TAG_MEDIA_GROUP).getJSONArray(
                        TAG_MEDIA_CONTENT);

                JSONObject mediaObj = (JSONObject) mediacontentArry
                        .get(0);

                String fullResolutionUrl = mediaObj
                        .getString(TAG_IMG_URL);

                // image full resolution widht and height
                final int width = mediaObj.getInt(TAG_IMG_WIDTH);
                final int height = mediaObj.getInt(TAG_IMG_HEIGHT);

                Log.d(TAG, "Full resolution image. url: "
                        + fullResolutionUrl + ", w: " + width
                        + ", h: " + height);

                ImageLoader imageLoader = AppController
                        .getInstance().getImageLoader();

                // We download image into ImageView instead of
                // NetworkImageView to have callback methods
                // Currently NetworkImageView doesn't have callback
                // methods

                imageLoader.get(fullResolutionUrl,
                        new ImageLoader.ImageListener() {

                            @Override
                            public void onErrorResponse(
                                    VolleyError arg0) {
                                Toast.makeText(
                                        getApplicationContext(),
                                        getString(R.string.msg_wall_fetch_error),
                                        Toast.LENGTH_LONG).show();
                            }

                            @Override
                            public void onResponse(
                                    ImageLoader.ImageContainer response,
                                    boolean arg1) {
                                if (response.getBitmap() != null) {
                                    // load bitmap into imageview
                                    backdrop
                                            .setImageBitmap(response
                                                    .getBitmap());
                                    }
                            }
                        });

            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(),
                        getString(R.string.msg_unknown_error),
                        Toast.LENGTH_LONG).show();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Error: " + error.getMessage());
            // unable to fetch wallpapers
            // either google username is wrong or
            // devices doesn't have internet connection
            Toast.makeText(getApplicationContext(),
                    getString(R.string.msg_wall_fetch_error),
                    Toast.LENGTH_LONG).show();

        }
    });

    // Remove the url from cache
    AppController.getInstance().getRequestQueue().getCache().remove(url);

    // Disable the cache for this url, so that it always fetches updated
    // json
    jsonObjReq.setShouldCache(false);

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq);
}

所以我想的可能是以某种方式我可以在AppBar中显示Image on Next活动将是已经加载到网格视图中的相同网络图像(缩略图),因此它可以很快。 / p>

因此,我要问的是显示从下一个Activity的NetworkImageView中的gridView中选择的相同图像。

1 个答案:

答案 0 :(得分:0)

您必须对图像进行缓存才能重复使用它们。请完成this 杰克为链接中的缓存提供了一个漂亮的解决方案。希望它有所帮助。