如何参考本地图像

时间:2012-05-08 16:30:16

标签: android android-arrayadapter

我正在使用LazyList代码加载外部没有问题的图像。我创建了我的应用程序的“虚拟”版本,不需要连接,因此我将所有文件都本地化。如何调整此代码以获取存储在我的drawable文件夹中的本地图像而不是http url?

imageLoader.DisplayImage(urls[position], mContext, vidImg_iv);

public void DisplayImage(String url, Context context, ImageView imageView)
    {
        imageViews.put(imageView, url);
        Bitmap bitmap=memoryCache.get(url);
        if(bitmap!=null) {
            imageView.setImageBitmap(bitmap);
        } else {
            queuePhoto(url, context, imageView);
            imageView.setImageResource(stub_id);
        }    
    }

如果我不使用下面的ImageLoader类,我的内存不足了:

if (urls[position].contains("1612802193_1625181163001_20120507051900-national")) {
                vidImg_iv.setImageResource(R.drawable.national_still);
            }
            else if (urls[position].toString().contains("1612802193_1625188510001_20120507050000-sports")) {
                vidImg_iv.setImageResource(R.drawable.sports_still);
            }
            else if (urls[position].toString().contains("1612802193_1625189005001_20120507050000-national-sp")) {
                vidImg_iv.setImageResource(R.drawable.national_spanish_still);
            }
            else if (urls[position].contains("1612802193_1625255029001_20120507080000-snap")) {
                vidImg_iv.setImageResource(R.drawable.snap_still);
            }
            else if (urls[position].contains("1612802193_1625256948001_20120507075800-breaking")) {
                vidImg_iv.setImageResource(R.drawable.breaking_still);
            }
            else if (urls[position].contains("1612802193_1625308288001_20120507080800-vbmarguh")) {
                vidImg_iv.setImageResource(R.drawable.vbmarguh_still);
            }
            else if (urls[position].contains("1612802193_1625308309001_20120507083200-extreme")) {
                vidImg_iv.setImageResource(R.drawable.extreme_still);
            }
            else if (urls[position].contains("1612802193_1625188512001_20120507050000-sports")) {
                vidImg_iv.setImageResource(R.drawable.sports_still);
            }

1 个答案:

答案 0 :(得分:0)

//Load a bitmap from local resources
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.some_image);
imageView.setImageBitmap(bitmap);

//Load an image from the assets folder
AssetManager assetManager = context.getAssets();
BufferedInputStream buf = new BufferedInputStream(assetManager.open("filename"));
Bitmap bitmap = BitmapFactory.decodeStream(buf);
buf.close();
imageView.setImageBitmap(bitmap);