开发者

How to display image from Url in android?

开发者 https://www.devze.com 2023-03-13 05:57 出处:网络
Hello I have set properdata and my code is working but dont know what wrong images not display in gridview it only display black darkable images. Here is my code:

Hello I have set properdata and my code is working but dont know what wrong images not display in gridview it only display black darkable images. Here is my code:

String response=sb.toString();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is1 = new InputSource();
        is1.setCharacterStream(new StringReader(response));
        Document doc = db.parse(is1);
        Element root = doc.getDocumentElement();

        NodeList nodes = root.getElementsByTagName("Photo");
        for (int i = 0; i < nodes.getLength(); i++) 
        {
            Element element = (Element) nodes.item(i);

            if(trmap.containsKey(strPhotoId))
            {

                trmap.clear();
            }
            if(trmapother.containsKey(strphotoidother))
            {
                trmapother.clear();
            }

            NodeList ProfilePhoto = element.getElementsByTagName("ProfilePhoto");
            Element line1 = (Element) ProfilePhoto.item(0);
            photoimg= getCharacterDataFromElement(line1);

            NodeList Count = element.getElementsByTagName("Count");
            Element line2 = (Element) Count.item(0);
            strCount= getCharacterDataFromElement(line2);

            for(int k=0; k<Integer.parseInt(strCount); k++)
            {
                NodeList PhotoId = element.getElementsByTagName("PhotoId");
                Element line4 = (Element) PhotoId.item(k);
                strPhotoId= Integer.parseInt(getCharacterDataFromElement(line4));
                ar.add(strPhotoId);

                NodeList PhotoUrl = element.getElementsByTagName("PhotoUrl");
                Element line5 = (Element) PhotoUrl.item(k);
                strPhotoUrl= getCharacterDataFromElement(line5);
                arrprofile.add(strPhotoUrl);


                trmap.put(strPhotoId, strPhotoUrl); 
                trmap.containsKey(strPhotoId);
                //trmap.containsKey(strPhotoUrl);
                //trmap.get(strPhotoUrl);
                //trmap.clear();
            }


            NodeList OtherPhoto = element.getElementsByTagName("OtherPhoto");
            Element line6 = (Element) OtherPhoto.item(0);
            photoother= getCharacterDataFromElement(line6);

            NodeList Countother = element.getElementsByTagName("Count");
            Element line7 = (Element) Countother.item(1);
            strCountother= getCharacterDataFromElement(line7);

            NodeList UserPhotoother = element.getElementsByTagName("UserPhoto");
            Element line8 = (Element) UserPhotoother.item(1);
            String strUserPhoto1= getCharacterDataFromElement(line8);
            for(int S=0; S<Integer.parseInt(strCountother); S++)
            {

                NodeList PhotoIdother = element.getElementsByTagName("PhotoId");
                Element line9 = (Element) PhotoIdother.item(S);
                strphotoidother= Integer.parseInt(getCharacterDataFromElement(开发者_开发知识库line9));
                br.add(strphotoidother);

                NodeList PhotoUrlother = element.getElementsByTagName("PhotoUrl");
                Element line10 = (Element) PhotoUrlother.item(S);
                strPhotoUrlother= getCharacterDataFromElement(line10);
                arrother.add(strPhotoUrlother);

                trmapother.put(strphotoidother,strPhotoUrlother);
                trmapother.containsKey(strphotoidother);
                //trmapother.clear();

            }


            /*NodeList PrivatePhoto = element.getElementsByTagName("PrivatePhoto");
            Element line11 = (Element) PrivatePhoto.item(0);
            strcountprivate= getCharacterDataFromElement(line11);


            NodeList Countprivate = element.getElementsByTagName("Count");
            Element line12 = (Element) Countprivate.item(2);
            strCountotherprivate= getCharacterDataFromElement(line12);

            NodeList UserPhotoprivate = element.getElementsByTagName("UserPhoto");
            Element line13 = (Element) UserPhotoprivate.item(2);
            String strUserPhotoprivate= getCharacterDataFromElement(line13);

            if(trmaprivate.containsKey(strPhotoIdprivate))
            {
                trmaprivate.clear();
            }

            for(int M =0; M<Integer.parseInt(strUserPhotoprivate); M++)
            {
            NodeList PhotoIdprivate = element.getElementsByTagName("PhotoId");
            Element line14 = (Element) PhotoIdprivate.item(M);
           strPhotoIdprivate= Integer.parseInt(getCharacterDataFromElement(line14));
           br.add(strPhotoIdprivate);

            NodeList PhotoUrlprivate = element.getElementsByTagName("PhotoUrl");
            Element line15 = (Element) PhotoUrlprivate.item(M);
            strPhotoUrlprivate= getCharacterDataFromElement(line15);
            arrprivate.add(strUserPhotoprivate);

            trmaprivate.put(strPhotoIdprivate, strPhotoUrlprivate);

            }*/
            //StringTokenizer st1 = new StringTokenizer(strCount, ",");

            TableLayout phototbl = (TableLayout)findViewById(R.id.imhttbl);

            LinearLayout workline = new LinearLayout(this);
            workline.setBackgroundResource(R.drawable.filterbarbackground);


            LinearLayout workline4 = new LinearLayout(this);


            TextView textView1 =new TextView(this);
            textView1.setPadding(5,5,0,0);
            textView1.setText("Photo Of You");
            textView1.setTextColor(Color.BLACK);

            workline4.addView(textView1);
            workline.addView(workline4);

            phototbl.addView(workline);

            LinearLayout lin = new LinearLayout(this);
            lin.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,250));
            GridView gridView11 = new GridView(this); 
            gridView11.setBackgroundColor(Color.WHITE);
            gridView11.setVerticalSpacing(10);
            gridView11.setHorizontalSpacing(10);
            gridView11.setNumColumns(4);
            gridView11.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
            gridView11.setAdapter(new ImageAdapter(this)); 
            gridView11.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT));
            gridView11.setOnItemClickListener(new OnItemClickListener() 
            {

                public void onItemClick(AdapterView parent, View v, int position, long id)
                {
                    strPhotoId=position;
                    if(trmap.containsKey(position))
                    {
                        strPhotoId=position;
                        Intent intent = new Intent(Photopart.this,ImageSwit.class);
                        startActivity(intent);
                    }

                }
            });
            //scr.addView(gridView11);
            lin.addView(gridView11);
            phototbl.addView(lin);

            LinearLayout phototxt = new LinearLayout(this);
            phototxt.setBackgroundColor(Color.WHITE);
            TextView textViewtxt =new TextView(this);
            textViewtxt.setPadding(5,5,0,0);
            textViewtxt.setText("Photo with only you in them, in which you can be easily identified and your face is clear visible");
            phototxt.addView(textViewtxt);
            phototbl.addView(phototxt);


            LinearLayout photoll = new LinearLayout(this);
            photoll.setBackgroundResource(R.drawable.filterbarbackground);


            LinearLayout photooth = new LinearLayout(this);


            TextView textView2 =new TextView(this);
            textView2.setPadding(5,5,0,0);
            textView2.setText("Other Photo");
            textView2.setTextColor(Color.BLACK);

            photooth.addView(textView2);
            photoll.addView(photooth);

            phototbl.addView(photoll);

            LinearLayout linothr = new LinearLayout(this);
            ScrollView scd = new ScrollView(this);
            GridView gridView22 = new GridView(this);
            gridView22.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
            gridView22.setVerticalSpacing(10);
            gridView22.setHorizontalSpacing(10);
            gridView22.setNumColumns(4);
            gridView22.setBackgroundColor(Color.WHITE);
            gridView22.setAdapter(new ImageAdapter1(this)); 
            gridView22.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,200));
            gridView22.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView parent, View v, int position, long id)
                {

                    strphotoidother=position;
                    if(trmapother.containsKey(position))
                    {
                        strphotoidother=position;
                        startActivity(new Intent(Photopart.this,ImageSwit.class));
                    }
                }
            });
            //scd.addView(gridView22);
            linothr.addView(gridView22);
            phototbl.addView(linothr);

            LinearLayout phototxt1 = new LinearLayout(this);
            phototxt1.setBackgroundColor(Color.WHITE);
            TextView textViewtxt1 =new TextView(this);
            textViewtxt1.setPadding(5,5,0,0);
            textViewtxt1.setText("All other photos of you and your friends party pics,travels pics etc...");
            phototxt1.addView(textViewtxt1);
            phototbl.addView(phototxt1);

            LinearLayout photollprovate = new LinearLayout(this);
            photollprovate.setBackgroundResource(R.drawable.filterbarbackground);


            LinearLayout photoothprivate = new LinearLayout(this);


            TextView textView3 =new TextView(this);
            textView3.setPadding(5,5,0,0);
            textView3.setText("Private Photo");
            textView3.setTextColor(Color.BLACK);

            photoothprivate.addView(textView3);
            photollprovate.addView(photoothprivate);

            phototbl.addView(photollprovate);

            LinearLayout linothrprivate = new LinearLayout(this);
            ScrollView scg = new ScrollView(this);
            GridView gridView33 = new GridView(this);
            gridView33.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
            gridView33.setVerticalSpacing(10);
            gridView33.setHorizontalSpacing(10);
            gridView33.setNumColumns(4);
            gridView33.setBackgroundColor(Color.WHITE);
            //gridView33.setAdapter(new ImageAdapter2(this));
            gridView33.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,200));
            gridView33.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView parent, View v, int position, long id) 
                {


                }
            });
            //scg.addView(gridView33);
            linothrprivate.addView(gridView33);
            phototbl.addView(linothrprivate);

            LinearLayout phototxt2 = new LinearLayout(this);
            phototxt2.setBackgroundColor(Color.WHITE);
            TextView textViewtxt2 =new TextView(this);
            textViewtxt2.setPadding(5,5,0,0);
            textViewtxt2.setText("Private photo will be only available to users that has allowed access by your self.");
            phototxt2.addView(textViewtxt2);
            phototbl.addView(phototxt2);

        }
    }

    catch (IOException e)
    {
        e.printStackTrace();
    }
    catch (ParserConfigurationException e) 
    {
        e.printStackTrace();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
    }

}
public static String getCharacterDataFromElement(Element e) 
{
   Node child = e.getFirstChild();
   if (child instanceof CharacterData) {
     CharacterData cd = (CharacterData) child;
       return cd.getData();
     }
   return "?";
 }
/*public class ImageAdapter1 extends BaseAdapter 
{
    private Context context;

    public ImageAdapter1(Context c) 
    {
        context = c;
    }

    //---returns the number of images---
    public int getCount() {
        return imageIDs.length;
    }

    //---returns the ID of an item--- 
    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setPadding(5, 5, 5, 5);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(imageIDs[position]);
        return imageView;
    }
} */  
public class ImageAdapter extends BaseAdapter 
{
    int mGalleryItemBackground;
    private Context context; 
    public ImageAdapter(Context c) 
    {
        context = c;
        TypedArray a = obtainStyledAttributes(R.styleable.gellery);
        mGalleryItemBackground = a.getResourceId(R.styleable.gellery_android_galleryItemBackground, 0);
        a.recycle();
    }
    //---returns the number of images---
    public int getCount()
    {
        return trmap.size();
    }
    //---returns the ID of an item--- 
    public Object getItem(int position)
    {
        return trmap.get(position);
    }

    public long getItemId(int position)
    {
        return position;
    } 
    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        if (convertView == null) 
        {  
            iv = new ImageView(context);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setLayoutParams(new GridView.LayoutParams(85,85));
            iv.setPadding(2,2,2,2);
            iv.setBackgroundResource(mGalleryItemBackground);
        }
        else
        {
            iv = (ImageView)convertView;

        }
        //iv.setImageResource(imageIDs[position]);
       if(trmap.containsKey(position))
        {
         try
          {

          URL FileUrl= new URL(trmap.get(position));   

          URL myUrl = new URL(trmap.get(strPhotoUrl));
          String dt = myUrl.toString();
          HttpURLConnection conn= (HttpURLConnection)myUrl .openConnection();
         // conn.setDoInput(true);
          //conn.connect();
          HttpGet httpRequest = null;

          try 
          {
                  httpRequest = new HttpGet(FileUrl.toURI());
          }
          catch (URISyntaxException e) 
          {
                  e.printStackTrace();
          }

          HttpClient httpclient = new DefaultHttpClient();
          HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

          HttpEntity entity = response.getEntity();
          BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
          InputStream instream = bufHttpEntity.getContent();

          bmImg = BitmapFactory.decodeStream(instream); 
          //InputStream is = conn.getInputStream();

          //bmImg = BitmapFactory.decodeStream(is);  
           }               
           catch(Exception e)
           {
           }
        }
       iv.setImageBitmap(bmImg);
       return iv;


Your code is so lengthy, I can't read all but, I can suggest you to try using this code just pass the URL and

    Drawable drawable_from_url(String url, String src_name) throws java.net.MalformedURLException, java.io.IOException      {         
return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).awagetContent()), src_name);    
 } 


Just use the following method to draw image from url:

Drawable drawable_from_url(String url, String src_name) throws java.net.MalformedURLException, java.io.IOException

{
    return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).awagetContent()), src_name);
}

Just pass the string url to the method(and for src_name any string ) and it will return you a drawable object, then use setBckgroundDrawable() method of the imageview to set the background of the image.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号