【问题标题】:Loading an Image into Imageview将图像加载到 Imageview
【发布时间】:2013-07-19 06:18:18
【问题描述】:

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

 imgLoader.DisplayImage (....);

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

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

  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}]

【问题讨论】:

  • 你的图片是否正确显示在浏览器中
  • @Stacks28 是的,我可以在我保存的本地主机路径中看到我的图像
  • 你的清单中是否有写外部存储权限。

标签: android json image-loading


【解决方案1】:

AlbumsActivity.java 中试试这个代码:

ImageView thumb_image = (ImageView) findViewById(R.id.list_image);                  
ImageLoader imgLoader = new ImageLoader(getApplicationContext());      
imgLoader.DisplayImage(TAG_IMAGE, thumb_image);

【讨论】:

  • 我在这方面遇到了问题,我的 PHP 代码在 JSON 中编码了这个路径: - http:/\/\localhost/\images/\adele.png 这会造成获取图像的问题
  • 嗨,当我想使用本地主机图像路径时,我遇到了同样的问题
  • 使用 imageloader 我曾尝试从内容提供商、本地设备 sdcard、项目中的可绘制文件夹、远程服务器的 url 加载图像。所以,我的问题是我能否从 localhost 类型的服务器获取图像就像在同一台开发机器上运行的 xampp 服务器一样。
【解决方案2】:

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;
    }

}

【讨论】:

  • 投反对票,因为它似乎不是答案。这应该在评论部分。
  • @ManishAndroid 对不起那个兄弟.. 但我已经改变了答案。
【解决方案3】:
                            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);

【讨论】:

    猜你喜欢
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 2019-06-27
    • 1970-01-01
    • 2020-09-02
    相关资源
    最近更新 更多