Android 大量图像的位图下载导致问题

Android 大量图像的位图下载导致问题,android,bitmap,Android,Bitmap,我正在学习安卓开发,并且正在开发一个应用程序,使用一个有趣的工具 我的用户可以搜索特定的卡片,现在我希望实现一个卡片显示器,它按卡片类型显示卡片,并允许用户向右或向左滑动,以显示MyJSONArray中的下一张卡片。我的API请求给我一个,每个JSONObject都有一个img属性,带有卡片图像URL 因此,当用户刷卡时,我会执行以下操作: // Swipe right -> number - 1 (Previous page) // Swipe left -> number +

我正在学习安卓开发,并且正在开发一个应用程序,使用一个有趣的工具

我的用户可以搜索特定的卡片,现在我希望实现一个
卡片显示器
,它按卡片类型显示卡片,并允许用户向右或向左滑动,以显示My
JSONArray
中的下一张卡片。我的API请求给我一个,每个
JSONObject
都有一个
img
属性,带有卡片图像
URL

因此,当用户刷卡时,我会执行以下操作:

// Swipe right -> number - 1 (Previous page) 
// Swipe left -> number + 1 (Next page)
public void displayCardNumber(int number) {
    APIRequests apiRequests = new APIRequests();

    try {
        // Gets the JSONObject at 'number' and retrieves its img URL.
        JSONObject card = (JSONObject) cardsArray.get(number);
        String currentImageURL = (String) card.get("img");

        // Here is where my problem is.
        Bitmap bitmap = apiRequests.getImageBitmap(currentImageURL);
        if (bitmap != null) {
            setNewImage(bitmap);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

}
 // Load image, decode it to Bitmap and display Bitmap in ImageView
 ImageLoader imageLoader = ImageLoader.getInstance();
 ImageView i = (ImageView)view.findViewById(R.id.cardDisplay);

 imageLoader.displayImage(currentImageURL, i);
但是
apirest.getImageBitmap(URL)
是我遇到问题的地方

我必须下载图像才能显示它,但当我下载多个图像时,下面的代码块不仅不起作用,我还必须找到一种有效的方式来显示我的卡(可能需要后台下载?)

为什么我不能下载多张图片?我获取
位图的方法是否错误


谢谢。

我找到了一个解决方案

为了加载并显示图像,我现在使用一个名为的库,该库允许我执行以下操作:

// Swipe right -> number - 1 (Previous page) 
// Swipe left -> number + 1 (Next page)
public void displayCardNumber(int number) {
    APIRequests apiRequests = new APIRequests();

    try {
        // Gets the JSONObject at 'number' and retrieves its img URL.
        JSONObject card = (JSONObject) cardsArray.get(number);
        String currentImageURL = (String) card.get("img");

        // Here is where my problem is.
        Bitmap bitmap = apiRequests.getImageBitmap(currentImageURL);
        if (bitmap != null) {
            setNewImage(bitmap);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

}
 // Load image, decode it to Bitmap and display Bitmap in ImageView
 ImageLoader imageLoader = ImageLoader.getInstance();
 ImageView i = (ImageView)view.findViewById(R.id.cardDisplay);

 imageLoader.displayImage(currentImageURL, i);
我在
AsyncTask
doInBackground
中检索到正确的URL,并使用库的方法显示它。

使用Glide。这会更快

 Bitmap theBitmap = Glide.
    with(this).
    load(imgUrl). //image url as string
    asBitmap().
    into(100, 100). // Width and height
    get();
您也可以通过以下方式使用它:

         Glide.with(context).load(url) // image url
                .thumbnail(0.5f)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(imgId) // If no image found the imgId is the default image id for the imageView
                .into(imageView); // imageView to show the image

我可以在两天内接受我自己的答案,我一定会接受。这是解决你问题的办法。这不是你问题的答案。接受它不是一个好主意。公平地说,这个库确实处理了我的问题,但我不知道为什么我不能加载2个位图。有没有办法用这个库给我的图片一个滑动效果?到目前为止,它还可以工作,但当我刷下一张卡时,它会消失,并且需要大约1秒的时间来显示一张新卡,我想用动画或其他东西来填补这个空白。是的,您可以将自定义动画对象添加到此库中:Glide.with(context).load(imgUrl.animation(animation).into(imageView);//动画可以是自定义的animator对象,也可以是资源中的动画//