2010-11-19 9 views

risposta

25

È possibile utilizzare il metodo setImageDrawable

ImageView iv = new ImageView; 

URL url = new URL(address); 
InputStream content = (InputStream)url.getContent(); 
Drawable d = Drawable.createFromStream(content , "src"); 
iv.setImageDrawable(d) 

[2014/12/16] Edit: Utilizzando Picasso, rende la vita molto più semplice

String url = "http://i.imgur.com/bIRGzVO.jpg"; 
ImageView iv = new ImageView; 

Picasso.with(context).load(url).into(iv); 
//Picasso.with(context).load(url).centerCrop().fit().into(iv); 
+0

grazie mille, ma l'applicazione è diventata molto lenta nell'esecuzione ??! – Adham

+0

qual'è la ragione ?? – Adham

+0

L'applicazione è diventata molto lenta durante l'esecuzione, perché molto probabilmente si sta eseguendo questo codice sul thread principale. Sposta il codice nel thread in background per velocizzare l'app – Arkde

4

Penso che sia possibile utilizzare il metodo setImageUri. L'URI può essere creato usando Uri.parse.

+0

scusa per questa domanda, ma come posso rendere l'oggetto URI per la mia stringa di collegamento ?? – Adham

+2

aggiunto questo dettaglio nella mia risposta –

1

prima u bisogno di colpire url dell'immagine e negozio il server Dati come array di byte, quindi è necessario convertire questi dati di byte in immagine Bitmap. Ecco il codice

   String myfeed="http://174.136.1.35/dev/atmsearch/visa.jpg"; 

       try{ 

        URL url=new URL(myfeed); 
        URLConnection connection=url.openConnection(); 
        connection.setDoOutput(true); 
        connection.setDoOutput(true); 
        connection.setRequestProperty("METHOD", "POST"); 
        connection.setRequestProperty("Content-Type","application/x-www-from-urlencoded"); 

        HttpURLConnection httpConnection=(HttpURLConnection)connection; 

        int responsecode=httpConnection.getResponseCode(); 

        if(responsecode==HttpURLConnection.HTTP_OK){ 
         InputStream in=((URLConnection)httpConnection).getInputStream(); 
         int len=0; 
         Bitmap b=BitmapFactory.decodeStream(in); 



         System.out.println(b.toString()); 


         byte[] data1=new byte[1024]; 

         while(-1!=(len=in.read(data1))){ 
          System.out.println("--input stream--"); 
          datafromserver.append(new String(data1,0,len)); 

         } 
         //System.out.println(datafromserver); 
        } 

       }catch(IOException e){ 
        System.out.println("Error...."+e); 
        //Toast.makeText(context, text, duration) 

       } 

// Ora imposta l'immagine bitmap nella vista immagine imageview.setImageBitmap (b);

+0

whats datafromserver ?? – Adham

+0

datafromserver è un generatore di stringhe .. se non usi u otterrai anche l'immagine nella variabile b. prova a inserire b nella visualizzazione dell'immagine. –

0

prima colpito il link dell'immagine, allora si otterrà l'immagine come byte array.Now solo decodificare l'array di byte per image.Lets bitmap dare un'occhiata:

package Image.Read.a; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 

import android.graphics.BitmapFactory; 

public class Connecetion1 
{ 
public void setNetwork() 
{ 
    try 
    { 

     URL url = new URL("http://3.bp.blogspot.com/_9UYLMDqrnnE/S4UgSrTt8LI/AAAAAAAADxI/drlWsmQ8HW0/s400/sachin_tendulkar_double_century.jpg"); 

     URLConnection connection=url.openConnection(); 

     HttpURLConnection HCon=(HttpURLConnection)connection; 

     int ResCode=HCon.getResponseCode(); 

     System.out.println("Responce Code is = "+ResCode); 


     if(ResCode==HttpURLConnection.HTTP_OK) 
     { 

     InputStream ins=((URLConnection)HCon).getInputStream(); 

       Data.StoreImg=BitmapFactory.decodeStream(ins); 


     } 

    } 
    catch (MalformedURLException e) 
     { 

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

      e.printStackTrace(); 
     } 

} 

}

È possibile ottenere il tutorial completo da http://www.androidcookers.blogspot.com/2011/06/retrieve-image-from-internet.html

Problemi correlati