将图像加载到Imageview中

时间:2013-07-19 06:18:18

标签: android json image-loading

我在尝试调用 ImageLoader的DisplayImage函数时遇到问题,实际上没有得到我需要使用来显示图像

 imgLoader.DisplayImage (....);

我正在编写一个应用程序,其中我需要将图像加载到ImageView中,但总是空白而不是图像,请参见下图:

enter image description here

注意:我不知道我需要使用什么,在这一行:

  ImageLoader imgLoader = new ImageLoader(getApplicationContext());      
  imgLoader.DisplayImage(url, loader, imageView);

所以请告诉我代码,我需要写什么才能在ImageView中显示图像, 根据我的代码

我正在使用Localhost来获取图片,请参阅以下代码以获取图片 -

private static final String URL_ALBUMS = "http://10.0.2.2/songs/albums.php";
private static final String TAG_IMAGE = "imagepath";

String image = c.getString(TAG_IMAGE);

HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_IMAGE, image);

protected void onPostExecute(String file_url) {
    // dismiss the dialog after getting all albums
    pDialog.dismiss();
    // updating UI from Background Thread
    runOnUiThread(new Runnable() {
    public void run() {
    /**
     * Updating parsed JSON data into ListView
     * */
    ListAdapter adapter = new SimpleAdapter(

    AlbumsActivity.this, albumsList,
    R.layout.list_item_albums, new String[] { TAG_ID,
    TAG_NAME, TAG_IMAGE, TAG_SONGS_COUNT }, 

    new int[] {
    R.id.album_id, R.id.album_name, R.id.list_image, R.id.songs_count });

    // ImageLoader class instance
    ImageLoader imgLoader = new ImageLoader(getApplicationContext());

    // here i don't know how to call DisplayImage function   
    imgLoader.DisplayImage (....); // what to write here    

    // updating listview
    setListAdapter(adapter);
         }
    });

注意 - 我在我的项目中添加了所有必需的类,类是:

      ImageLoader.java, FileCache.java, MemoryCache.java & Utils.java

data.php: -

 1 => array(
        "id" => 1,
        "album" => "127 Hours",
        "imageurl" => "images/onetwentyseven.png"
         ............

注意: - 正如您所看到的,我正在使用本地图像,那些存储 localhost in images 文件夹。

JSON: -

[{"id":1,"name":"127 Hours","imagepath":"images\/onetwentyseven.png","songs_count":2},{"id":2,"name":"Adele 21","imagepath":"images\/adele.png","songs_count":2}]

3 个答案:

答案 0 :(得分:2)

AlbumsActivity.java

中试用此代码
ImageView thumb_image = (ImageView) findViewById(R.id.list_image);                  
ImageLoader imgLoader = new ImageLoader(getApplicationContext());      
imgLoader.DisplayImage(TAG_IMAGE, thumb_image);

答案 1 :(得分:1)

Url =“您的图片网址”// ex: - www.abc.com/images/img.jpg

Drawable drawable = LoadImageFromWebOperations(Url);
  imageView.setImageDrawable(drawable);

private Drawable LoadImageFromWebOperations(String url) {

    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        System.out.println("Exc=" + e);
        return null;
    }

}

答案 2 :(得分:1)

                            imageLoader = ImageLoader.getInstance();
            configuration = ImageLoaderConfiguration
                    .createDefault(getActivity());
            imageLoader.init(configuration);

            options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.default_image)
            .showImageForEmptyUri(R.drawable.default_image)
            .showImageOnFail(R.drawable.default_image)
            .cacheInMemory().cacheOnDisc()
            .bitmapConfig(Bitmap.Config.RGB_565).build();

            imageLoader.displayImage(user_bean.getmImage(), mProfileImage,
                    options);