【问题标题】:Show Image From URL in Android?在 Android 中显示来自 URL 的图像?
【发布时间】:2014-03-22 13:03:01
【问题描述】:

如何将图像 URL 转换为任何可显示的类型,我想用我的 CustomAdapter 显示它,就像下面的代码中的“Category_Name”和“Category_Description”一样;

JSONArray jsonResponse = new JSONArray(result); // Result is my JSON
                asd = new String[3][jsonResponse.length()];
                rowItems = new ArrayList<RowItem>();
                for (int i = 0; i < jsonResponse.length(); i++) {
                    JSONObject js = jsonResponse.getJSONObject(i);
asd[0][i]= js.getString("Category_Name"); // Fetch Category Name
asd[1][i]= js.getString("Category_Description"); // Fetch Category Description
asd[2][i]= js.getString("Image"); // Fetch Image URL

RowItem item = new RowItem(R.drawable.abc_ab_bottom_solid_dark_holo, asd[0][i], asd[1][i]); 
// I delivered asd[0][i] as a title and asd[1][0] as a post content..
rowItems.add(item);
                }
adapter = new CustomListViewAdapter(MainActivity.this,R.layout.list_item, rowItems);
                    listView.setAdapter(adapter);

如何使用 Fetched Image URL 并像标题、帖子内容一样显示它。

自定义适配器;

private class ViewHolder {
        ImageView imageView;
        TextView txtTitle;
        TextView txtDesc;
    }
public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        RowItem rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
            holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.txtDesc.setText(rowItem.getDesc()); // RowItem Setter Getter Method
        holder.txtTitle.setText(rowItem.getTitle()); 
        holder.imageView.setImageResource(rowItem.getImageId());

        return convertView;
    }
}

【问题讨论】:

  • 检查this。描述的很详细

标签: android android-listview android-imageview android-arrayadapter android-custom-view


【解决方案1】:

您必须下载图像并将其设置为 imageView

下载并设置图片:

public void downloadImageFromUrl(ImageView iv, String path){
    InputStream in =null;
        Bitmap bmp=null;
        int responseCode = -1;
        try{
        URL url = new URL(path);
                HttpURLConnection con = (HttpURLConnection)url.openConnection();
                con.setDoInput(true);
                con.connect();
                responseCode = con.getResponseCode();
                if(responseCode == HttpURLConnection.HTTP_OK)
                {
                    //download the image
                    in = con.getInputStream();
                    bmp = BitmapFactory.decodeStream(in);
                    in.close();
                    if(bmp!=null)
                           iv.setImageBitmap(bmp);
                }

    }
        catch(Exception e){
            Log.e("Exception",e.printStackTrace());
    }
}

【讨论】:

  • 我的返回值是多少?我在哪里以及如何定义这个方法并将值取回给 onpostexecute(),你的答案太差了。
  • 这只是您从服务器下载图像时缺少的基本答案。仅供参考,此方法接受 imageView 引用的输入,因此如果您调用此函数,它将为您设置图像。此外,如果您使用的是列表视图,请尝试使用惰性加载器。
【解决方案2】:

试试this。它在解析图像和制作我的自定义适配器方面帮助了我很多。希望它也能帮助你。

【讨论】:

  • 这对我没有帮助。它太复杂了我需要做的就是转换现有的 Url 并将它们发送到我的 CustomAdapter。无论如何,谢谢
猜你喜欢
  • 2011-09-23
  • 1970-01-01
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多