简体   繁体   English

在Android中显示来自URL的图像?

[英]Show Image From URL in Android?

How can I convert Image URL to any showable type, I want to show it with my CustomAdapter just like "Category_Name" and "Category_Description" as in the following code ; 如何将图像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);

How Can I use Fetched Image URL and show it just like title , post content. 如何使用提取的图像URL并将其显示为title,post内容。

Custom Adapter ; 定制适配器;

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

You will have to download the image and set it to the imageView 您将必须下载图像并将其设置为imageView

To download and set the image: 要下载并设置图像:

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

试试这个 。它帮了我很多的解析图像,使我的自定义adapter.Hope它也将帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM